在扩展广播接收器的类中使用getdefaultsharedpreferences

时间:2012-08-19 18:00:11

标签: android broadcastreceiver sharedpreferences alarmmanager

您好我正在尝试设置一个应用程序,其中存储在sharedPreferences中的某些值会每周重置一次。 我已经设置了警报管理器,以便在单击按钮时开始,如下所示:

public class EditPreferences extends PreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener, OnClickListener {

Button button;
Toast mToast;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.prefsbuttons);
button = (Button) findViewById(R.id.bPrefButton);
button.setOnClickListener(this);
addPreferencesFromResource(R.xml.preferences);
getPreferenceManager().getSharedPreferences().
registerOnSharedPreferenceChangeListener(this);


 }

 protected void onDestroy() {
 getPreferenceManager().getSharedPreferences().
unregisterOnSharedPreferenceChangeListener(this);

 @overide
 super.onDestroy();
 public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bPrefButton:
     Intent intent = new Intent(EditPreferences.this, RepeatingAlarm.class);
     PendingIntent sender = PendingIntent.getBroadcast(EditPreferences.this,
             0, intent, 0);
    SharedPreferences userPrefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    Editor editor = userPrefs.edit();
    editor.putInt("savedFlash23", 0);
    editor.putInt("savedFlash4", 0);
    editor.putInt("savedFlash5", 0);
    editor.putInt("savedClean23", 0);
    editor.putInt("savedClean4", 0);
    editor.putInt("savedClean5", 0);
    editor.putInt("savedDog23", 0);
    editor.putInt("savedDog4", 0);
    editor.putInt("savedDog5", 0);
    editor.commit();

     // We want the alarm to go off 30 seconds from now.
     long firstTime = SystemClock.elapsedRealtime();
     firstTime += 15*1000;

     // Schedule the alarm!
     AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
     am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                     firstTime, 15*1000, sender);

     // Tell the user about what we did.
     if (mToast != null) {
         mToast.cancel();
     }

     mToast = Toast.makeText(getBaseContext(), "first toast message",
             Toast.LENGTH_LONG);
     mToast.show();
 }


}



 } 

显示toast消息并重置共享首选项。

意图传递给Listener的类,我有以下代码:

public class RepeatingAlarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "repeat toast message",
            Toast.LENGTH_LONG).show();
    SharedPreferences userPrefs= PreferenceManager.getDefaultSharedPreferences(context);
    Editor editor = userPrefs.edit();
    editor.putInt("savedFlash23", 0);
    editor.putInt("savedFlash4", 0);
    editor.putInt("savedFlash5", 0);
    editor.putInt("savedClean23", 0);
    editor.putInt("savedClean4", 0);
    editor.putInt("savedClean5", 0);
    editor.putInt("savedDog23", 0);
    editor.putInt("savedDog4", 0);
    editor.putInt("savedDog5", 0);
    editor.commit();
}

}

显示Toast消息,但我的共享首选项中的值不再被重置。

1 个答案:

答案 0 :(得分:0)

解决方案似乎不是尝试在广播接收器中做一些事情。我使用以下代码启动了一个新的intent,它打开了一个扩展Activity的类,可以正常访问共享首选项。

Intent i = new Intent(context, Reset.class);

    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(i);