Tuesday 17 May 2016

Save the value by using shared preferences.

What is shared preferences?
  • You can save and retrieve key, value pair data from Shared preferences.
  • SharedPreferences values will persist across user sessions. Data in shared preferences will be persistent even though user closes the application. 
  • You can get values from Shared preferences using getSharedPreferences() method. 
  • You also need an editor to edit and save the changes in shared preferences. 
  • Use SharedPreferences to store data: booleans, floats, ints, longs, and strings.    

Create the Credentials class for save&get the value in sharepreferences.
 
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Laptop on 12-05-2016.
 */
public class Credentials {
    private static final String APP_KEY = "app_name";
    private static final String USER_ID = "user_id";
    private static final String USER_EMAIL = "email";

    public static void SaveUserID(int id, Context context) {
        SharedPreferences preferences = context.getSharedPreferences(APP_KEY, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(USER_ID, id);
        editor.apply();
    }

    public static int getUserID(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(APP_KEY, Activity.MODE_PRIVATE);
        return preferences.getInt(USER_ID, 0);
    }


    public static void SaveUserEmail(String email, Context context) {
        SharedPreferences preferences = context.getSharedPreferences(APP_KEY, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(USER_EMAIL, email);
        editor.apply();
    }

    public static String getUserEmail(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(APP_KEY, Activity.MODE_PRIVATE);
        return preferences.getString(USER_EMAIL, "");
    }
}

Create the Main file in project.


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.digi.soft.Utils.Credentials;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Laptop on 12-05-2016.
 */
public class MainActivity extends AppCompatActivity {

    public static final String LOGIN_URL = "http://employee.digisoftsolution.net/api/User/Login";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activtiy_login);
    }

    private void LoginUser() {

        StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int id = jsonObject.optInt("ID");
                            String email = jsonObject.optString("EmployeeEmail");

                            if (id > 0) {
                                //*******Save value through share prefernce*******//
                                Credentials.SaveUserID(id,MainActivity.this);
                                Credentials.SaveUserEmail(email, MainActivity.this);
                                openProfile();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("", "");
                return map;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
    private void openProfile() {
        Intent intent = new Intent(this, InOutActivity.class);
        startActivity(intent);
    }
    //*******************END*****************//

}


May this code help you. Thanks!!!!!!


                                                                                                                                              

No comments:

Post a Comment