Monday, 28 August 2017

Session Manage

We can Uses  2 Way

First  Way


Splash Class ----------------


public class Splash extends AppCompatActivity {
    private static int SPLASH_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedIns tanceState);
        setContentView(R.layout.activity_splash);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                if (Preferences.getBoolean(Preferences.IS_LOGIN)) {
                    Intent i = new Intent(Splash.this, MainActivity.class);
                    startActivity(i);
                } else {
                    Intent i = new Intent(Splash.this, LoginActivity.class);
                    startActivity(i);
                }


                // close this activity                finish();
            }
        }, SPLASH_TIME_OUT);
    }

}



class Preferences  --------------


public class Preferences {
    private static final String PREF_NAME = "com.app";
    public static final String ID = "id";
    public static final String AUTH_TOKEN = "auth_token";
    public static final String USER_NAME = "user_name";


    public static final String IS_LOGIN = "is_login";


    private static SharedPreferences sharedPref;

    public static void init(Context context) {
        sharedPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static void saveLoginInfo(LoginResponse loginResponse, boolean isLogin) {
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(ID, loginResponse.getId());
        editor.putString(AUTH_TOKEN, loginResponse.getAuth_token());
        editor.putString(USER_NAME, loginResponse.getUsername());
        editor.putBoolean(IS_LOGIN, isLogin);
        editor.apply();
    }

    public static void saveValue(String key, String value) {
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static void saveValue(String key, int value) {
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static void saveValue(String key, boolean value) {
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static String getString(String key) {
        return sharedPref.getString(key, null);
    }

    public static int getInt(String key) {
        return sharedPref.getInt(key, 0);
    }

    public static boolean getBoolean(String key) {
        return sharedPref.getBoolean(key, false);
    }

    public static void clearAll() {
        sharedPref.edit().clear().apply();
    }

    public static void clear(String key) {
        sharedPref.edit().remove(key).apply();
    }
}


On Login Success  ----------------

@Override
public void onLoginSuccess(LoginResponse loginResponce) {
    Preferences.saveLoginInfo(loginResponce, true);
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);



}



Log Out -----------------

case R.id.txtlogout:
    AlertDialog.Builder alert = new AlertDialog.Builder
(new ContextThemeWrapper(this, R.style.AlertDialogTheme));
    alert.setTitle("Logout");
    alert.setMessage("Do you want to logout?");
    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        }
    });
    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
    alert.show();
    break;





2  Way

SplashScreen  java Class-------------


public class SplashScreen extends AppCompatActivity {

    SessionManager manager;
    private static int SPLASH_TIME_OUT = 2000;
    private static Locale myLocale;
    SharedPreferences preferences;
    private static final String PREFRENCES_NAME = "myprefrences";
    private static final String Locale_KeyValue = "Saved Locale";
    private static SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        manager = new SessionManager();
        Thread background = new Thread() {
            public void run() {

                try {
                    sleep(3 * 1000);
                    String statuslogin = manager.getPreferences(SplashScreen.this, "statuslogin");
                    Log.d("statuslogin", statuslogin);
                    if (statuslogin.equals("1")) {
                        Intent i = new Intent(SplashScreen.this, MainActivity.class);
                        startActivity(i);
                    } else {
                        Intent i = new Intent(SplashScreen.this, LoginActivity.class);
                        startActivity(i);
                    }
                    finish();

                } catch (Exception e) {

                }
            }
        };
        background.start();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***        startActivity(intent);
        finish();
        System.exit(0);
    }
}




Login Activity Class --------------

On Response


if (status.equals("true")) {
    manager.setPreferences(LoginActivity.this, "statuslogin", "1");
    String statuslogin = manager.getPreferences(LoginActivity.this, "status");
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);
} else {
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}






Logout Class ------------


case R.id.txtlogout:
    AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogTheme));
    alert.setTitle("Logout");
    alert.setMessage("Do you want to logout?");
    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            manager.setPreferences(MainActivity.this, "statuslogin", "0");
            preferences = getSharedPreferences(PREFRENCES_NAME, Context.MODE_PRIVATE);
            preferences.edit().clear().commit();
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        }
    });
    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
    alert.show();
    break;


SessionManager Class  -----------


public class SessionManager {
    public void setPreferences(Context context, String key, String value) {
        SharedPreferences.Editor editor = context.getSharedPreferences("PREFRENCES_NAME", Context.MODE_PRIVATE).edit();
        editor.putString(key, value);
        editor.commit();

    }
    public String getPreferences(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences("PREFRENCES_NAME", Context.MODE_PRIVATE);
        String position = preferences.getString(key, "");
        return position;
    }
} 

No comments:

Post a Comment