Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android training day 4

1,406 views

Published on

Android Training Day 4 slide organized in Mobile Nepal from 22nd sept, 2013 to 28th sept, 2013 for beginner level

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Android training day 4

  1. 1. Android Apps Development – Training Day 4
  2. 2. Android Storage ● How do you save your application data ? – SharedPreferences* ● Storage in key-value pairs. – Internal Storage ● Storage on device memory – External Storage ● Storage on shared external storage – SQLite Databases* ● Store structured data in a private database – Network Connections ● Store data on web
  3. 3. SharedPreferences ● The way android stores users preferences ● But why SharedPreferences ? – Ease of implementation – Faster in terms of coding – Accessibility of key values – Suitable for the small things ● e.g. Game difficulty level, user name, user accounts, etc. I got your back buddy !
  4. 4. SharedPreferences ● Every thing is saved in terms of key-value – e.g. “NAME”, “Android” “PhoneNumber”, “1234” ● For the implementation we will be using the: – SharedPreferences ● General framework to save and retrieve persistent key-value pairs – Editor ● Interface used for modifying values in a SharedPreferences object ● Use of commit() or apply() to make changes persist
  5. 5. Implementation public class ProjectPreference{ SharedPreferences pref; Editor editor; Context _context; int PRIVATE_MODE = 0; private static final String PREF_NAME = “prefmyapp”; ... (A) public ProjectPreference(Context context){ this._context = context; pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); editor = pref.edit(); } ... (B) Accessing the SharedPreferences this way gives you more flexibility We are creating a separate class here
  6. 6. A: Define Variables ● Suppose we have a contact to save: – Name – Address – Number ● Declare them: public static final String KEY_NAME = “name”; public static final String KEY_ADD = “address”; public static final String KEY_NUM = “number”;
  7. 7. B: Define Getters and Setters ● Defining setters and getters – void for setters public void setName(String key, String value){ editor.putString(key, value); editor.commit(); } – <types> for getters public String getName(String key){ return pref.getString(key, “”); } You can define your default values here
  8. 8. Accessing SharedPreferences ● Create an object ProjectPreference SM; ● Initialize SM = new ProjectPreference(Class.this); ● Access variables and methods SM.getName(SM.KEY_NAME); ● Set values SM.setName(SM.KEY_NAME, “Android”);
  9. 9. Building the application
  10. 10. SQLite Database ● SQLite is innate in every Android device – No need for extra setup etc. ● We only have to define the SQL statements for creating and updating the database ● All commands regarding creating tables, inserting values etc. are the same as in SQL
  11. 11. SharedPreferenes VS SQLite ● SQLite provides capability of more complex storage – Storage for large number of data – Higher capabilities ● e.g. in the loadshedding app we notice that we do not have to update the app every time a new schedule arrives.
  12. 12. SQLite in Android ● Android should use a DBHelper – extends SQLiteOpenHelper class ● Two main methods: – onCreate() ● Creates the database, parameters needed: ● DATABASE_NAME ● DATABASE_VERSION – onUpgrade() ● Updates the current database depending upon: ● DATABASE_VERSION
  13. 13. ● All operations regarding the database is to be performed by the SQLiteDatabase object ● Either the: – getWritableDatabase() : Write mode – getReadableDatabase() : Read mode used with the SQLiteDatabase object. SQLiteDatabase db = this.getWritableDatabase() db.insert(...)
  14. 14. ● While using the SELECT operations we use the Cursor object – Cursor is something similar to an iterator if(cursor.moveToFirst()) { do { … } while(cursor.moveToLast()) } – Also during the query we use a rawQuery() method
  15. 15. ● In insert operations we use the ContentValues object. – ContentValues values = new ContentValues( ); values.put("name" , "ABC"); values.put("phoneNumber" , "123"); db.insert(table, null, values);
  16. 16. ● While programming create the following functions: – Getting a single row – Getting all rows – Getting row count – Updating row – Deleting row
  17. 17. For better SQLite implementation ● Create both: – Data Model class ● With all the setters and getters – Data Handler ● Extending the SQLiteOpenHelper
  18. 18. Implementing SQLite Insert user data into database Redirect user into another activity showing the database
  19. 19. AsyncTask ● What is threading ? – The way any computing device responds to its commands ● Android only uses a main thread by default for its application ● The main thread processes the following: – Application UI – User inputs to the application Is this enough?
  20. 20. ● While processing heavy/ time consuming task, the UI will freeze until the task is complete – Bad practice ● For these tasks open a separate thread – ASYNCTASK ● Asynctask is android's multitasker
  21. 21. ● Methods inside the AsyncTask – OnPreExecute() ● Generally used to load the progress bar – doInBackground(Params... ) ● All the logic is dumped here – OnProgressUpdate() ● Called when publishProgress() is called in the doInBackground() – onPostExecute(Result) ● Gives out the desired result
  22. 22. ● Using the AsyncTask – Extend the AsyncTask class Abc extends AsyncTask<Params,Progress,Result> ● Params: the input, what you pass into the AsyncTask ● Progress: on updates, passed to onProgressUpdate() ● Result: the output from doInBackground() returned to the onPostExecute() – Call on the AsyncTask new Abc.execute(Params);

×