I am developing an android app and I want it to do a particular action(say for example, go to a particular URL) when the user loads it for 'N' times. How do I go about doing it. I know its got to do with SharedPrefs and Activity LifeCycle but I am not being able to get a headstart into it. Can someone plz suggest how to proceed.
Use the onCreate method in your activity to set a counter in SharedPrefs. Increase it by 1 each time and when it reaches N, do your thing..
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
int count = sharedPreferences.getInt("count", 0);
if (count == N) {
...
} else {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("count", count + 1);
editor.commit();
}
Related
private boolean rightReviewTiming() {
int insertKitCnt = sharedPreferences.getInt("insert_kit_cnt",0);
insertKitCnt++;
sharedPreferences.edit().putInt("insert_kit_cnt", insertKitCnt);
sharedPreferences.edit().commit();
insertKitCnt = sharedPreferences.getInt("insert_kit_cnt", 0);
Log.d("ehhehe", "rightReviewTiming: " + insertKitCnt);
if((insertKitCnt % 11 == 0) && (insertKitCnt % 2 == 0)) {
return true;
} else {
return false;
}
}
I want to update insert_kit_cnt key. But, its log always shows '0'. If you know where is the issue, please let me know about that. I think maybe insert_kit_cnt commit is not working. Is commit timing wrong?
Do the commit or apply on the editor that you're making change on.
sharedPreferences.edit().putInt("insert_kit_cnt", insertKitCnt).apply();
You are creating two editor references. You need to use only one reference of editor:
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("insert_kit_cnt", insertKitCnt);
editor.apply();
Or in one-liner code, you can also do like this:
sharedPreferences.edit().putInt("insert_kit_cnt", insertKitCnt).apply();
I developed an android app in which I have added the backup option. Now, I want to add the functionality of prompting the user when they carried the last backup with date and time. And if they didnt do backup then allowing them to do backup.
Please anyone help me with this how to perform this operation?
Assuming you have a backup function:
public void backup(){
//get time of last backup
SharedPreferences sharedPref = context.getSharedPreferences(
"PrefsFile", Context.MODE_PRIVATE);
long time = System.currentTimeMillis();
long lastBackup = sharedPref.getLong("BackupTime", 0);
//if there was no previous backup, lastBackup will be 0
if (lastBackup != 0){
long timeDiff = time - lastBackup; //difference will be in milliseconds
//do what you need with the time difference
}
//code for backup
SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong("BackupTime", time); //update last backup time
editor.apply();
}
I changed shared preferences value but it still returns old one. What am I missing?
This code executed when the user clicks on the item in RecyclerView. So on the first click, I get message " this true" as expected. But on second click I also get " this true", but expect "this false".
SharedPreferences prefs = context.getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);
boolean value = prefs.getBoolean(KEY_PREF, true);
if (value) {
Log.v(LOG_TAG, "this true");
Log.v(LOG_TAG, "editing value..");
SharedPreferences.Editor prefs = context.getSharedPreferences(MY_PREF, MODE_PRIVATE).edit();
prefs.putBoolean(KEY_PREF, new_value);
prefs.apply();
} else {
Log.v(LOG_TAG, "this false");
}
All you store is true, always, so there's no way to show this false as it never gonna happen. In fact, your code will not compile as new_value is never declared not assigned.
PS: there's no sense to call getSharedPreferences() second time. You got it already in prefs prior entering your if() block.
The call prefs.apply is asynchronously. You may not see the immediate change. Instead you could use prefs.commit which is synchronously.
I'm developing a mobile app using ApacheCordova/Phonegap.
I need a function that sends a SMS to me once per install. If I put my function on "DeviceReady" it will be run each time the app opens.
Is there any solution for a function to be run when app is installed OR when it runs for first time?
Any suggestion would be appreciated.
Check if it is the first time with a method and then perform the action if that method determines that it is the first time.
Ex:
isFirstTime() Method
private boolean isFirstTime()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
// Send the SMS
}
return ranBefore;
}
You may want to add it to your onCreate()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topLevelLayout = findViewById(R.id.top_layout);
if (isFirstTime()) {
topLevelLayout.setVisibility(View.INVISIBLE);
}
I added a field to the localstorage and on startup just check if that field exists. So something like this:
if (window.localStorage.getItem("installed") == undefined) {
/* run function */
window.localStorage.setItem("installed", true);
}
Edit: The reason I prefer this over the other methods is that this works on iOS, WP, etc as well, and not only on android
This should be what u are searching for:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false))
{
// run your one time code
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
Use some boolean value if its true don't call that function other wise call that function example is here
if(smssent!=true)
{
//call sms sending method
}
else
{
//just leave blank or else notify user using some toast message
}
Note:-the boolean value store in some database like sharedprefernce or sqllite, files....
This might seem a little weird!!
But I was wondering if I could add some help on my app .which comes when You first open your app and when you touch the screen it vanishes(Only comes in first run).
The problem is I didn't even know what is this thing called I tried a search on google but I'm nowhere near it.
It will be very helpful if anyone can provide some info about that(what is it called ,How to add it in your App)
Thanks !!
Check my answer for some thing on first setup.
Put this on first time run.
shPref = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
SharedPreferences.Editor pref_editor = shPref.edit();
pref_editor.putBoolean("first_time", false);
pref_editor.commit();
Check it like this:
shPref = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
boolean isFirstRun = myPrefs.getBoolean("first_run",false);
if(isFirstRun){
txtViewHelp.setVisibility(View.VISIBLE);
//logic of making it invisible on touch outside
}
else
txtViewHelp.setVisibility(View.GONE);
Hope this helps.
Try this!
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
if(!previouslyStarted){
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
edit.commit();
**// when the application is previously opened
//Show your help Test**
}
else
{
**// when the application is opened very First Time**
Intent intent = new Intent(MainActivity.this, Home_Module.class);
MainActivity.this.startActivity(intent);
}
This will Help You.
It stores your application status that it is previously opened or not?