I´m trying to use SharedPreferences in a BroadcastReceiver to count how often onReceive is called. After 3 times the prefs saved value will be set to 0, also when the user starts the app, the value will be set to 0.
What works: When the Receiver starts I can count and increment the value in the onReceive. When I call my Method cancelAlarm(), now with the context from the receiver, i can set the prefs value to 0.
What doesn't: When the user opens the app, the prefs value should be set to 0, but I can't seem to set it with the activity context....
Receiver:
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences homeCarePrefs = context.getSharedPreferences("HomeCare", 0);
System.out.println("AlarmNotification.onRecive"+homeCarePrefs.getInt("count", 0));
SharedPreferences.Editor editor = homeCarePrefs.edit();
editor.putInt("count", homeCarePrefs.getInt("count", 0)+1);
editor.commit();
if(homeCarePrefs.getInt("count", 0) >= 3){
cancelAlarm(context); //this works!!
}
.....
public void cancelAlarm(Context context){
SharedPreferences homeCarePrefs = context.getSharedPreferences("HomeCare", 0);
System.out.println("AlarmNotification.cancleAlarm "+homeCarePrefs.getInt("count", 0));
SharedPreferences.Editor editor = homeCarePrefs.edit();
editor.putInt("count", 0);
editor.commit();
.....
Main
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
SharedPreferences homeCarePrefs = getSharedPreferences("HomeCare", 0);
System.out.println("AlarmNotification.cancleAlarm "+homeCarePrefs.getInt("count", 0));
SharedPreferences.Editor editor = homeCarePrefs.edit();
editor.putInt("count", 0);
editor.commit();
aN = new AlarmNotification();
aN.cancelAlarm(this);
aN.setAlarm(getApplicationContext(), person.getEvening());
Also, if I call cancelAlarm(this) from main it never sets the value to 0.
After 3 hours of trying I still can't understand why it's not working.
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.SLFK.homecare"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="AlarmNotification"
android:process=":remote" >
</receiver>
<activity
android:name="de.SLFK.homecare.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="screens.LoginScreen"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name="screens.MainScreen"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name="screens.WeightScreen"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name="screens.PulseScreen"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name="screens.SystolicScreen"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name="screens.DiastolicScreen"
android:configChanges="keyboardHidden|orientation|screenSize" />
</application>
</manifest>