1

I have an Activity that contains a button. When the user clicks on the button first I have to check whether the user is logged in or not. If the user is not logged in, then first the user has to log in. Then he sees some video. How can I do that?

Code:

findViewById(R.id.btn_dailyReward).setEnabled(true);
((Button)findViewById(R.id.btn_dailyReward)).setAlpha(0.5f);
View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if ((mSupersonicInstance.isRewardedVideoAvailable()))
            mSupersonicInstance.showRewardedVideo();
        else
            Toast.makeText(ControllerActivity.this
                ,getString(R.string.NO_VIDEO_ADS),Toast.LENGTH_LONG).show();
    }
};
findViewById(R.id.btn_dailyReward).setOnClickListener(onClickListener);
Nitin
  • 19
  • 2

3 Answers3

2

You need some backend services to support such functions, ideally. The service, may be Restful, exposes API for you to call, such that you can:

boolean isLogined = SomeServices.isLogined(userName, password, token...);

Many such good services include:

Update I: If you want to just do a simple fake, you can use a boolean variable to save/load info to and from SharedPreference.

First you need to save the boolean state into SharedPreference:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("IsUserLogined"), false);
editor.apply();  //Do not use editor.commit();Thanks,@HendraWijayaDjiono,

Then to read it back:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Boolean  isLoginned = sharedPref.getBoolean("IsUserLogined", defaultValue);

Update II: Note that share preference is NOT safe. If your app crashes, uninstalled, reinstalled, updated, the info you saved is lost. To solve this, you can resort to Sqlite or Realm DB to persist your information.

David
  • 15,894
  • 22
  • 55
  • 66
  • Well, sqlite data will be lost too if app uninstalled and the query can be failed if app crash :). And for this case(login), shared preferences is the best solution, since if you uninstall application, you need to re-login again. – HendraWD Aug 08 '16 at 03:54
  • You still use Editor.commit here, it makes the shared preferences not safe. Consider using Editor.apply instead of Editor.commit – HendraWD Aug 08 '16 at 03:56
  • @HendraWijayaDjiono, no, sqlite is portable and can be saved to external storage. Pls see if you agree upon this post: http://stackoverflow.com/questions/1995320/how-do-i-backup-a-database-file-to-the-sd-card-on-android – David Aug 08 '16 at 03:57
  • yes it is portable, but only if you make a copy of the database on external storage, and it's not a best practice, since user don't want any junk after uninstall an application – HendraWD Aug 08 '16 at 04:04
  • @HendraWijayaDjiono, what is the best practice then? Thanks! – David Aug 08 '16 at 04:05
  • Try not to leave any data behind when user uninstall your application – HendraWD Aug 08 '16 at 04:07
  • @HendraWijayaDjiono, no you misunderstood my question. I mean what is the best practice to keep these data to make it crash-safe? You mentioned db is not safe either, then ...? – David Aug 08 '16 at 04:11
  • I don't understand your question. What do you mean by crash-safe data? Crash is actually all about the logic include logic of how you save your data. If you save your data with shared preferences correctly, application will not crash. – HendraWD Aug 08 '16 at 04:26
  • forget it! if you trace back the comments, you will understand that. But if you don't understand, no way but forget it! – David Aug 08 '16 at 04:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120379/discussion-between-hendra-wijaya-djiono-and-david). – HendraWD Aug 08 '16 at 04:37
0

You can achieve this using SharedPreferences. It can be used to store some unsafe information.

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreference(context);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("LoggedInUser", "Mike");
editor.putBoolean("loggedin",true);
editor.commit();

And you can get it by

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreference(context);

boolean loggedIn = sharedPref.getBoolean("loggedin", false);
if(loggedIn){
String user = sharedPref.getString("LoggedInUser");}
Collins
  • 145
  • 2
  • 15
0

So you just need to save the login state to SharedPreferences. Usually on Android application development, we use token and refresh token to re-authenticate to server, so we don't need to get the state of login directly to server. It's same like if we save a cookie to a web browser about our login state if a user decide to check "Remember me"

This is how you save/load from shared preferences.

SharedPreferences sharedPreference = PreferenceManager.getDefaultSharedPreferences(mContext);

//how to save the login state
boolean loginState = true;
SharedPreferences.Editor e = sharedPreference.edit();
e.putBoolean("KEY_LOGIN", loginState);
e.apply();

//how to get the login state
boolean defaultValue = false;
sharedPreference.getBoolean("KEY_LOGIN", defaultValue);
HendraWD
  • 2,984
  • 2
  • 31
  • 45