Saturday, 1 September 2018

How to create SharedPreference in Android.

Create one class

here is complete code how to create SharedPreference
    public class SessionManager {
    SharedPreferences pref;
    SharedPreferences.Editor editor;// Editor for Shared preferences
    Context _context;
    int PRIVATE_MODE = 0;
    private static final String PREFERENCES = "Preferences";
    public static final String UNAME = "uname";
    public static final String PNAME = "pname";
    public static final String TIME = "time";
    public static final String DATE = "date";
    public static final String ADDRESS = "address";
    private static final String PREF_NAME = "newApp";
    public SessionManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void saveDEtails(String uname, String pname, String time,
        String date,String address) {
        editor.putString(UNAME, uname);
        editor.putString(PNAME, pname);
        editor.putString(TIME, time);
        editor.putString(DATE, date);
        editor.putString(ADDRESS, address);
        editor.commit();
    }
    public HashMap getUserDetails(){
        HashMap user = new HashMap();
        user.put(UNAME, pref.getString(UNAME, null));
        user.put(PNAME, pref.getString(PNAME, null));
        user.put(TIME, pref.getString(TIME, null));
        user.put(DATE, pref.getString(DATE, null));
        user.put(ADDRESS, pref.getString(ADDRESS, null));
        return user;
    }
}
// And where you want to stored the data in SharedPreference
SessionManager mSession;
mSession = new SessionManager(getApplicationContext());
mSession.saveDEtails(nameu,namep,time,weddingdate,add);
//Here you can get value from SharedPreference
SessionManager mSession;
HashMap user;
mSession = new SessionManager(getApplicationContext());
user = mSession.getUserDetails();
String  username = user.get(SessionManager.UNAME); // get name
String  partnername = user.get(SessionManager.PNAME); // get email
String  time = user.get(SessionManager.TIME);
String date = user.get(SessionManager.DATE);
String address = user.get(SessionManager.ADDRESS);

Tuesday, 19 April 2016

Content provider in android

Content provider manage access to the structured set of data.they encapsulate the data and provide mechanism to define the data security. Content providers are the standard interface that connect data in a process with code running in another process.

When you want to access data from content provider you use the object content resolver in context application to communicate with the provider as a costumer. 

Android Application Components

There are four android components.
1. Activity
2. Service
3. Content Provider
4. Broadcast Receivers

Activity: Activity is nothing but the user interacting screen. Which user can interact with app.

Services: It handle background process of android without  disturbing user. And it is long running process.

Content Provider: This handle data and Database Management issue .

Broadcast Receiver :It handled the communication between android OS and Android Application.