SharedPreferences不保存或什么错误?

时间:2015-06-10 10:42:46

标签: android sharedpreferences

我试图在BroadcastReceiver中使用SharedPreferences来计算onReceive的调用频率。 3次后,prefs保存的值将设置为0,同样当用户启动应用程序时,该值将设置为0.。

什么有效: 当Receiver启动时,我可以计算并递增onReceive中的值。当我调用我的方法cancelAlarm()时,现在使用接收器的上下文,我可以将prefs值设置为0。

什么不是: 当用户打开应用程序时,prefs值应设置为0,但我似乎无法使用活动上下文设置它....

接收器:

    @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();
.....

主要

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());

另外,如果我从main调用cancelAlarm(this),它永远不会将值设置为0。

经过3个小时的尝试后,我仍然无法理解为什么它不起作用。

清单

<?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>

0 个答案:

没有答案