So basically, my service displays a notification which can be turned off if the user wishes.
The setting is controlled from within the application and is set through a PreferenceActivity.
When exiting the activity:
public void onDestroy(){
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
debug.postLog(Log.DEBUG, "Settings", "("+this.toString()+") showNotification: " + prefs.getBoolean("showNotification", true));
Shows the correct value, if the user unchecks the checkbox, it shows false and vice versa.
Meanwhile, when I call this at any time in my service:
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Boolean show = prefs.getBoolean("showNotification", true);
debug.postLog(Log.DEBUG, "Passive Service", "("+this.toString()+") showNotification: " + prefs.getBoolean("showNotification", true));
if(prefs.getBoolean("showNotification", true)){
I get the same result until the app is fully stopped then restarted, then the current value is kept. It is almost like the service gets a snapshot of the preferences.
What am I missing?
Solved, the service was running as remote.
Related
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 have created android app that require users to enter their phone numbers when they first use the app, now i am storing that info using SQL lite.the problem is every time they open the app it requires their phone number, and I want the app to just automatically login without asking for the phone number again, kind of like whats app.
Its not about landing page change. The activity which has a category "LAUNCHER" in manifest file always opens first. In that Activity .java file , You can make a check , whether the value for user is available in sqlite or not. if available perform intent to next page .. Check this link too ......Android check user logged in before, else start login activity
Either set a splash activity or another blank activity as your initial activity.
Then store a boolean in your app's shared preference to identify whether the application
is loading first time or not. Based on that boolean value, move to phone number entry screen or
your desired screen.
In your "login" activity you should look for the number in the db, if it is there, you open a new activity, if not, you ask the user. You can use setVisibility(int) in your "ask" views to not show them while looking in the db, and then, if you don't find the number, you show them.
You have to use sharedPrefrences where you can store whether user has stored his number.
When user open the app for the first time and enter his number, then store value in sharedprefrences.
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("prefrence", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("value", "selected"); editor.commit();
When user open the app, Splash screen will check the value in sharedprefrences. If user already had put his number then he will be re-directed to home screen rather than phone number screen.
Splash Screen:
Context mContext;
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mContext = SplashScreen.this;
new Handler().postDelayed(new Runnable() {
/*Showing splash screen with a timer. This will be useful when you
want to show case your app logo / company*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String value = (shared.getString("value", ""));
if(value!=null && !value.equals("")){
/*Re-Direct to Home Screen after Login*/
Intent intent = new Intent(mContext,MainActivity.class);
startActivity(intent);
}
else{
Intent intent = new Intent(mContext, LoginActivity.class);
startActivity(intent);
}
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
I want to implement voice actions, "start running" and "stop running".
"Start running" works fine, but "stop running" doesn't work.
My app has one activity with several fragments.
When I speak "stop running", the activity is destroyed and created. My workout data is lost.
setRetainInstance(true) has no effect.
Change launchMode to singleTask/singleTop/singleInstance has no effect.
I saved workout data in onSaveInstanceState(), but it's lost when a new activity is created.
Is there any other way?
I'm not sure if this is too obvious, but have you considered using SharedPreferences?
To save data:
sPref = getPreferences(MODE_PRIVATE);
testNum = 2;
SharedPreferences.Editor editor = sPref.edit();
editor.putInt("testName", testNum);
editor.commit();
To get data:
sPref = getPreferences(MODE_PRIVATE);
int retrievedTestNum = sPref.getInt("testName",-1);
System.out.println("The number you saved was " + retrievedTestNum + "!");
You can also save data in arrays and store it in the SharePreferences.
If this is too simple for you, you can grab a mySQL database, which is much more robust.
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?