I want to check my android app first running. So my code is follwing.
This code is successfully executed when my app is first installed.
But, i'm not sure it is executed when my app is updated through App Store.
Is it executed when my android app is updated through App Store?
public boolean CheckAppFirstExecute(){
SharedPreferences pref = getSharedPreferences("isFirst" , Activity.MODE_PRIVATE);
boolean isFirst = pref.getBoolean("isFirst", false);
if(!isFirst) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isFirst", true);
editor.commit();
....
}else{
Log.d("this is not first", "not first");
}
return !isFirst;
}
The shared preferences are not reset during an update. So your "first use" code will not run again.
Yes, the values in SharedPreference will not be dropped after an update through the Playstore or any manual apk install with install -r xx.apk
Related
I am creating a project where I have a Registration screen, which is used for user to Register into the
Application. This Register screen should only be visible the first time, so the user can fill it and log in, but when user opens the application at the second time the application must show show Registration screen (only for registered user).
I don't understand how to do this.
I'm saving user credential in the code
private void setLoginData(String mobile, String pin, String emailId)
{
SharedPreferences pref = getActivity().getSharedPreferences("LoginInfo", 0);
Editor editor;
editor = pref.edit();
editor.putString("mobileNumebr", mobile);
editor.putString("pin", pin);
editor.putString("emailId", emailId);
editor.apply();
}
Your Code :
private void setLoginData(String mobile, String pin, String emailId)
{
SharedPreferences pref = getActivity().getSharedPreferences("LoginInfo", 0);
Editor editor;
editor = pref.edit();
editor.putString("mobileNumebr", mobile);
editor.putString("pin", pin);
editor.putString("emailId", emailId);
editor.putBoolean("login",true);
editor.apply();
}
Above I have added one boolean shared preference parameter. Then we'll
use that in Splash Screen for checking that user has already
registered or login or not.
on Splash Screen check this condition,
SharedPreferences prefs = getActivity().getSharedPreferences("LoginInfo", 0);
// then you use
boolean isLogin = prefs.getBoolean("login", false);
if(isLogin){
// Go to the Main Activity
}else{
// Go to the Registration Activity
}
Hope it will help you my friend!
https://stuff.mit.edu/afs/sipb/project/android/docs/training/id-auth/custom_auth.html The Account manager is a bit tricky It will collect credentials for accounts associated with your app only once and handle authentication transparently from then on if no exceptions occur. If lets say the wife changed credentials on her device and forgot to inform you, well Account manager will prompt you for the new password. The user can even go to the phone settings/accounts and access things that way once you implement Account Manager. The above is strictly for the local device but chances are you want to actually use credentials stored on a local device to authenticate with a remote server. if this is the case check this out https://stuff.mit.edu/afs/sipb/project/android/docs/training/id-auth/custom_auth.html?q=authenticationactivity#q=authenticationactivity
I am making an Android project. In that project i want a screen to appear only during the app installation and not every time we start the app. For eg. in whatsapp the page in which we put our name appears only during installation. I want exactly that type of a screen. I am fairly new to android programming so any help will be appreciated. Thanks! Cheers!
You can use the SharedPreferences to identify if it is the "First time" the app is launched.
Just use a Boolean variable ("app_first_time") and change its value to false after your task runs for the first time.
final String PREFS_NAME = "PrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("app_first_time", true)) {
//here the app is being launched for first time, launch your screen
settings.edit().putBoolean("app_first_time", false).commit();
}
My app was working great until I got it ready for deployment. I have a portion of my app that checks to see if a checkmark is checked in the preferences. Well since I added as a library and am running through another application (created a free version of the app and trying to keep my code as a library) it always returns false.
SharedPreferences appPrefs = context.getSharedPreferences("com.company.widget_preferences", Context.MODE_PRIVATE);
boolean blNotifications;
blNotifications = appPrefs.getBoolean("notifications_new_message", false);
if (blNotifications)
{
//always returns false
}
Thanks for your help.
Figured it out guys. For some reason using SharedPreferences appPrefs = context.getSharedPreferences worked in dev, but as soon as I made it into a Library it didn't like it anymore. The correct way to call a default sharepreference is the following.
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
String value = pref.getString("name_of_my_pref", "default_value");
Thanks again.
I have this code that should read an unset preference on the first run:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences settings = getPreferences(MODE_PRIVATE);
firstTime = settings.getBoolean("firstTime", true);
Log.d("mything", "firstTime returns as: " + firstTime);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstTime", false);
editor.commit();
the variable "firstTime" is always returned as false.
I am uninstalling my app and loading it afresh.
Can someone explain?
Thanks in advance
Are you using Samsung Galaxy S with 2.2.1 firmware? There is known bug that shared preferences are not being removed if application is uninstalled. For example see comments here
Hmm strange. I run your code and work as expected. The first time you run it is logs
05-10 14:53:59.390: DEBUG/mything(4895): firstTime returns as: true
and if you run it again it always logs
05-10 14:55:25.780: DEBUG/mything(4895): firstTime returns as: false
Are you sure you are not missing something in the log ?
I want to show a message after the first launch of the app telling about the new features, each time a user installs a new version.
How do I do that?
Thanks in advance
I used this. It was posted by another stackoverflow member I don't recall who. Sorry. Call this code from onCreate()
// Show changelog
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
PackageInfo pInfo;
try {
pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
if (prefs.getLong("lastRunVersionCode", 0) < pInfo.versionCode) {
showDialog(DIALOG_CHANGELOG);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("lastRunVersionCode", pInfo.versionCode);
editor.commit();
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Error reading versionCode");
e.printStackTrace();
}
Have an activity for this, that checks if a preference has the value of the current version. If so proceed to the next activity. Otherwise störe the current version in the preference, show the what's new screen and then on button press procees to the next activity
That's simple.
On the first start show an AlertDialog and create a record in SharedPreferences. Whatever - flag or a string, or somewhat.
The next time your application is started just check that you have this flag in preferences. If there is no one, then this is a first start and it's time to show a dialog =)
You have to save somewhere a value which you can check on each startup whether the app was already started. SharePreferences would be a option. Or within a database.
if the user clears application data in the device settings then this all gets reset.