User authentication should be only once - android

I am developing an android application which should authenticate user only once in his mobile after installing the app. It should not ask the details for the second time. For this I have used shared preferences by setting a Boolean value. But it,s not working. Is there any suggestions here.. thanks friends !! My code is here
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("Register", true);
editor.commit();

use it this way :
in your onCreate() use :
if (isFirstTime()) {
// do what you want to do only once
}
to call the below :
private boolean isFirstTime()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
// first time
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
}
return !ranBefore;
}

Your approach of using SharedPreference is correct.Put the below coad snippet in your oncreate() method of the Activity.
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
register = prefs.getBoolean("Register",false);
if(!register)
{
//register user
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("Register", register);
editor.commit();
}
else{
//continue with the rest
}

the first time :
settings.edit().putBoolean("Register", true).commit();
and in onCreate you do the test
if(!settings.getBoolean("Register",false))
//it isn't the first time
So the full code :
SharedPreferences settings = getSharedPreferences("settings", 0);
if(!settings.getBoolean("Register",false)){
//it isn't the first time
}
else{
settings.edit().putBoolean("Register", true).commit();
}

Related

Don't show Intent after first load application

If you can help me I will be very grateful,
i want to launch an intent only in first load of my android application and save same settings. To save my settings I use SharedPreferences :
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();
edit.putBoolean("isFirstRun", false);
edit.commit();
Thanks a lot.
you can do something like this
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();
if(preferences.getBoolean("isFirstRun", true)){
edit.putBoolean("isFirstRun", false);
edit.commit();
//Do your stuff for first Run
}else {
}
In your activity A, you should check in your shared preference like:
if(!pref.getBoolean("isFirstRun", false)){
//Load activity B
// put your code for updating shared pref as you mention above.
}else{
//Load activity C
}

Where to clean SharedPrefererences when user closed app directly?

I'm new to android and java. I tried SharedPreferences to manage session. Once I login into screen I writing below code.
SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("login", true);
editor.commit();
When I closed the app and started again, it directly going to home screen without asking the credentials.
I dont know where to write these below lines:
SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
I want to clear the SharedPreferences even if the app closed directly. I tried with finalize but it didn't worked.
EDIT:
I add below code to my file. It is working but giving error.
code:
protected void onStop() {
SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("login", false);
editor.commit();
}
Error:
android.app.SuperNotCalledException: Activity {com.ec.testtab/com.ec.testtab.Tabs} did not call through to super.onStop()
Better to set prefs to false at your app closed. and when you come back again you just need to check this prefs value true or false.
false--- means not login
true---- means logined
I have handeled such things inside these two methods of activity they will be called in your case too so give a try:
#Override
protected void onResume() {
super.onResume();
// Start Logging
}
#Override
protected void onPause() {
super.onPause();
// End Logging
}
If you don't want to store shared preferences while your app is getting closed, you should clear your shared preferences every time your app is getting started, so that no previous shared preferences will be there while doing your app's further processes.
Once user logged-in into app with valid credentials then only changed share preferences login value to true.
SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("login", true);
editor.commit();
And every time on Oncreate just check "login" value to determine whether user is already logged-in or not.
You can add this lines of code
SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("login", false);
editor.commit();
when your app starts before fetching the username and password from the preferences. So that every time you start the app it will show login page.
Sharedpreferences is used for persistent storage, if u don't need store information for next launcher, use public static boolean isLogin maybe an easy way:
public static boolean sIsLogin = false;
...
if(doLogin()){
sIsLogin = true;
}
...
When u closed the app and started again, sIsLogin is false.

How to display only one time Login and then after start application directly in android

I am getting trouble in making only one time login... My aim is first user gets login screen.. If he is new user he will register and then login...from then when ever user starts the application he should directly redirect to main activity that is to skip the login page..please friends help me out from this problem..please post me any tutorials or any code...please tell me how to modify in manifest file also...
I am using like this in login activity but i didn't achieve my task.
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();
editor.putString("register","true");
editor.commit();
String getStatus=pref.getString("register", "nil");
if(getStatus.equals("true"))
// redirect to next activity
else
// show registration page again
Implement your SharedPreferences this way:
Boolean isFirstTime;
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(Splash.this);
SharedPreferences.Editor editor = app_preferences.edit();
isFirstTime = app_preferences.getBoolean("isFirstTime", true);
if (isFirstTime) {
//implement your first time logic
editor.putBoolean("isFirstTime", false);
editor.commit();
}else{
//app open directly
}
check it here
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
A very good example of session management in android app.
Use SharedPreferences. contains which tell an key is present or not in SharedPreferences. Change your code as:
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();
if(pref.contains("register"))
{
String getStatus=pref.getString("register", "nil");
if(getStatus.equals("true")){
redirect to next activity
}else{
//first time
editor.putString("register","true");
editor.commit();
/// show registration page again
}
}
else{ //first time
editor = pref.edit();
editor.putString("register","true");
editor.commit();
/// show registration page again
}
You can visit my blog
http://upadhyayjiteshandroid.blogspot.in/2013/01/android-working-with-shared-preferences.html
hope you will get the answer and understanding clearly
Boolean flag;
SharedPreferences applicationpreferences = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor editor = applicationpreferences .edit();
flag = applicationpreferences .getBoolean("flag", false);
if (flag) {
///second time activity
}else{
//first time
editor.putBoolean("flag", true);
editor.commit();
}
Check out the Session Management in Android which shows you the way how you can manage the login if user is already logged into the application or not. And switch the user accordingly.
Hope this will help you.
1.for storing in stored preferences use this
SharedPreferences.Editor editor = getSharedPreferences("DeviceToken",MODE_PRIVATE).edit();
editor.putString("DeviceTokenkey","ABABABABABABABB12345");
editor.apply();
2.for retrieving the same use
SharedPreferences prefs = getSharedPreferences("DeviceToken",
MODE_PRIVATE);
String deviceToken = prefs.getString("DeviceTokenkey", null);

Delete data from shared preferences in android

I am trying to delete the data from shared preferences. But I cant do that. To track that the data is removed or not, I am using this code:
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
SharedPreferences prefs = getSharedPreferences(share_pref_file, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.remove(share_pref_file);
editor.clear();
editor.commit();
getApplicationContext().getSharedPreferences(share_pref_file, 0).edit().clear().commit();
String strJson = prefs.getString("jsondata","");
if(strJson != null)
{
Log.d("CLEAR", "cccccccccccccccccccccccccccc");
}
userFunctions.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(),LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
});
But in logcat window it is showing me "cccccccccccccccccccccccccccc" value every time.
So can anyone help me out that how to remove/delete the data from shared preferences so that if I click on 'logout' button it will remove all the stored data? Thanks in advance.
An empty string is still a string (and String("") != null will return true). Try this instead:
if(!strJson.equals(""))
This assumes the empty string will never be a valid input in your SharedPreferences in the first place.
Try this one inside the button click event to clear the SharedPreferences values.
Editor editor = getSharedPreferences("MyPref", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
Try editor.clear(); followed by a editor.commit();
Here is one example that I've used:
Preference clearPref = (Preference) findPreference("MyPref");
btnLogout.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
Toast.makeText(getBaseContext(), "All data cleared!", Toast.LENGTH_SHORT).show();
return true;
}
});
Either change this:
String strJson = prefs.getString("jsondata", "");
To:
String strJson = prefs.getString("jsondata", null);
And then check:
if(strJson != null) {
Log.d("CLEAR", "cccccccccccccccccccccccccccc");
}
Or keep it as it is and check it like this:
if(strJson.equals("")) {
Log.d("CLEAR", "cccccccccccccccccccccccccccc");
}
settings.edit().clear().commit();
is a one line code I am using, works perfectly
It is my solution:
You can put null instead of your favorite data in shareprefrences
getApplicationContext();
SharedPreferences.Editor pref = (Editor) getSharedPreferences("data", MODE_PRIVATE).edit();
pref.putString("admin_no", null);
pref.commit();

Run AsyncTask only once when application starts for the first time

I want to run AsyncTask in android only once when application starts for the first time.
I tried to put Shared preference in onCreate but it didn't work. Any other ideas ?
SharedPreferences prefscrt = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefscrt.getBoolean("firstTime", false)) {
Log.d("DownloadManager", "Installing First Time");
new Task().execute(); //THIS WON'T WORK
SharedPreferences.Editor editor = prefscrt.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
Thanks in advance
Madz
Try this code :
SharedPreferences prefs = getSharedPreferences("firstTime", MODE_PRIVATE);
registartion = prefs.getBoolean("firstTime", false);
and now put your if condition
1.First check the value is null or not if null then sharedpref use and save them..
2.But in second time again check if they have already sharedpref run your code .
private Boolean firstTime = null;
SharedPreferences mPreferences = this.getSharedPreferences("first_time", Context.MODE_PRIVATE);
firstTime = mPreferences.getBoolean("firstTime", true);
private boolean isFirstTime() {
if(firstTime == null) {
SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean("firstTime", false);
editor.commit();
new Task().execute();
if (firstTime!=null) {
}
}
return firstTime;
}

Categories

Resources