Run certain operations only when the app is first installed - android

Let us presume that we have a function defined this way:
public class MyClass {
public static void RunFirstTime() {
//...
}
}
The very first time I install the app, I want to run RunFirstTime() but then any other time I run the app, that function should not run.
Is there a built-in way to do this?

Just use SharedPreferences and store a boolean variable when you are done with first run.
To be more clear, the code should be something like this:
public static void RunFirstTime() {
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
boolean firstRun = sPref.getBoolean("firstRun", true);
if(firstRun){
//Do something here
....
//Save firstRun = false in order to not repeat next time
Editor prefEdit = sPref.edit();
prefEdit.putBoolean("firstRun", false);
prefEdit.commit();
}
}

Try code like this:
public boolean isFirstRun() {
SharedPreferences sPref;
sPref = getPreferences(MODE_PRIVATE);
boolean isFirstRun = sPref.getBoolean("FirstRun", true);
Editor ed = sPref.edit();
ed.putBoolean("FirstRun", false);
ed.commit();
return isFirstRun;
}

Related

Save SWITCH button state, and recover state with SharedPrefs

I have a Settings class so the user can decide to subscribe/unsubscribe to channels in Parse Push.
I think I got it all figure out except for the part to recover, and maintain the switch state next time user open the app or changes the state.
Can someone please help me on how to save the state, and switch the SWITCH to what the user selected?
public class Settings extends Activity {
/**
* Called when the activity is first created.
*/
private Switch krspush, egspush;
public static final String PREFS_NAME = "SwitchButton";
krspush = (Switch) findViewById(R.id.krspush);
egspush = (Switch) findViewById(R.id.egspush);
SharedPreferences sharedPrefs = getSharedPreferences("SwitchButton", MODE_PRIVATE);
// How?
public void onKrsClick (View view) {
boolean on = ((Switch) view).isChecked();
if (on) {
SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
editor.putBoolean("onKrsClick", true);
editor.commit();
ParsePush.subscribeInBackground("egersund");
} else {
SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
editor.putBoolean("onKrsClick", false);
editor.commit();
ParsePush.unsubscribeInBackground("egersund");
}
}
public void onEgsClick (View view) {
boolean on = ((Switch) view).isChecked();
if (on) {
SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
editor.putBoolean("onEgsClick", true);
editor.commit();
ParsePush.subscribeInBackground("egersund");
} else {
SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
editor.putBoolean("onEgsClick", false);
editor.commit();
ParsePush.unsubscribeInBackground("egersund");
}
}
Override the onCreate method of that activity class and attempt to load the values you saved in SharedPreferences.
krspush.setChecked(sharedPrefs.getBoolean("onKrsClick",false));
findviewbyid will crash unless called after the view is created ie, in the oncreate method.
Consider using click listener on your switches.
I don't see the point of this line of code "SharedPreferences sharedPrefs = getSharedPreferences("SwitchButton", MODE_PRIVATE)"
Here is how you use shared preferences : https://stackoverflow.com/a/23024962/2590252
You better look into some samples to learn about best coding practices http://developer.android.com/samples/index.html

Android issue with boolean on shared preferences

Android issue with boolean on shared preferences
SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
boolean isSlide=spref.getBoolean("SLIDE_SHOW",false);
//in isSlide am getting the right value but the if statement is not working
if(isSlide==true){
//if true the page will slide but it works in false too
}
i tried in this link Issue with boolean on shared preferences but the answer is not clear
settings code:
<PreferenceCategory android:title="Slide Show" >
<CheckBoxPreference android:title="Slide Show" android:key="SLIDE_SHOW" />
</PreferenceCategory>
You may want to check if your SharedPreference variables are active. This is an example on how to set the SharedPreference.
To write into SharedPreference:
SharedPreferences prefs = getSharedPreferences("myPref", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("myBool", true);
editor.commit();
To read from SharedPreference:
SharedPreferences prefs = getSharedPreferences("myPref", 0);
if (prefs.getBoolean("myBool", false)) { //myBool is true; }
sorry i will not post the question correctly
The problem is not with preference it returns correct value the problem is in handler which is used to slide the viewpager based on the value which is returned by preference
the handler is running even though the activity is killed below the code solved my problem
SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
boolean isSlide=spref.getBoolean("SLIDE_SHOW",false);
if(isSlide==true)
{
SlideShowTimer();
}
public void SlideShowTimer()
{
mFilterTask = new Runnable() {
#Override
public void run() {
int i=0;
itemPosition = viewPager.getCurrentItem() + 1;
if(itemPosition!=10)
{
if(i <= adapter.getCount()-1)
{
viewPager.setCurrentItem(itemPosition,true);
mHandler.postDelayed(mFilterTask, 3000);
i++;
}
}
else
{
//ViewPagerAdapter.mediaPlayer.stop();
ViewPagerActivity.this.finish();
}
}
};
mHandler.removeCallbacks(mFilterTask);
mHandler.postDelayed(mFilterTask,3000);
}
#Override
public void onDestroy(){
super.onDestroy();
mHandler.removeCallbacks(mFilterTask);
}

Loading up value from SharedPreferences

In my Application class, I have the following:
public void loadPrefs(){
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean soundValue = sharedPrefs.getBoolean("SOUND", false);
}
public void savePrefs(String key, boolean value){
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sharedPrefs.edit();
edit.putBoolean(key, value);
edit.commit();
}
All OK there as far as I can tell.
Now in my main class which extends SurfaceView, I have the following:
myApp mySettings = (myApp)getContext().getApplicationContext();
Then I can save values like so: (Again from my surfaceView activity)
myPlanSettings.savePrefs("SOUND", false);}
However, what I just cannot work out is how to read the value back so I can set a local variable like so:
Boolean thisValue = (the value from the shared preferences).
Thanks for any help
Try this
public Object loadPrefs(String key,String defaul){
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
return sharedPrefs.get(key,default);
}
Cast it with the type while using getting the value
Basic usage:
boolean b=(Boolean)loadPrefs("value","default value")

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;
}

Can't access SharedPreferences from my Service

What I'm trying to do
Hello Guys.
I got a Service which, set's a boolean to true or to false whenever the service is started or stopped (started = true / stopped = false) in the SharedPreference. Now when I try to get the Boolean out there in my Activity, it allways dosn't find it. How can I solve this... Here's the Code for you Guys.
Code
Methode in my Service:
private void setStarted(boolean started) {
// SharedPreferences casten
mPrefs = this.getSharedPreferences(LOG_TAG, MODE_PRIVATE);
// Boolean in SharedPreferences hinzufügen
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear().apply();
editor.putBoolean(PREF_STARTED, started).commit();
editor.commit();
//mPrefs.edit().putBoolean(PREF_STARTED, started).commit();
Log.d(LOG_TAG, "Variabel " + mPrefs.getBoolean(PREF_STARTED, false));
}
In my Activity
// mPrefs caten
mPrefs = this.getSharedPreferences(GPSService.LOG_TAG, MODE_PRIVATE);
// boolean holen ob service gestartet oder nicht
run = mPrefs.getBoolean(GPSService.PREF_STARTED, false);
How do I get the boolean out of there? It allways returns me the default value I had to give in the getBoolean Methode.
Thanks for your help in advance
safari
Here is some code that I'm successfully using in one of my apps. It is used in various parts of the app, e.g. both from activities and services:
void putValue(Context context, String pref, boolean value) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(pref, value);
editor.commit();
}
boolean getValue(Context context, String value, boolean defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
return settings.getBoolean(value, defaultValue);
}
Try using getDefaultSharedPreferences(Context context) method of PreferenceManager in both your services and your activities.
private void setStarted(boolean started) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean(PREF_STARTED, started).commit();
Log.d(LOG_TAG, "Variabel " + mPrefs.getBoolean(PREF_STARTED, false));
}
In your Activity
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
run = mPrefs.getBoolean(GPSService.PREF_STARTED, false);
Also make sure you :
Never call .clear() on editor.
You use PreferenceManager.getDefaultSharedPreferences(this) everywhere.
Just build your prefs object the same way you would as part of your activity. The only difference is pulling your application's context in.
SharedPreferences prefs = _ getApplicationContext().getSharedPreferences("com.example.appname", Activity.MODE_PRIVATE);
Boolean tmpBool = prefs.getBoolean("PREF_NAME", null);

Categories

Resources