0

我有这个在固定时间执行 AlarmReceiver 的类(在 android 启动后开始)。我添加了 sharedpreferences 来禁用和启用警报。如果我禁用并重新启用警报不会运行。我需要重启模拟器。我可以避免模拟器重启吗?谢谢!

public class AutoStart extends BroadcastReceiver {

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    CheckboxPreference = prefs.getBoolean("checkboxPref", true);
Calendar cal4 = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
    cal4.set(Calendar.DATE,31);
    cal4.set(Calendar.MONTH,Calendar.DECEMBER); 
    cal4.set(Calendar.YEAR,2012);//year...
    cal4.set(Calendar.HOUR_OF_DAY, 23);  
    cal4.set(Calendar.MINUTE, 59);       
    cal4.set(Calendar.SECOND, 00);       

Intent intent2 = new Intent(context, AlarmReceiver.class);
    PendingIntent Alarm1 = PendingIntent.getBroadcast(context, 0, intent2, 0);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, Alarm1);

显现:

<receiver android:name="AutoStart">
        <intent-filter>
        <action
        android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.HOME" />
        </intent-filter>
        </receiver>
4

2 回答 2

1

您要做的第一件事是创建一个静态方法,使 Alarm 将其放入AutoStart

public static modifyAlarm (Context context, boolean cancel)
{
  Calendar cal4 = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
  cal4.set(Calendar.DATE,31);
  cal4.set(Calendar.MONTH,Calendar.DECEMBER); 
  cal4.set(Calendar.YEAR,2012);//year...
  cal4.set(Calendar.HOUR_OF_DAY, 23);  
  cal4.set(Calendar.MINUTE, 59);       
  cal4.set(Calendar.SECOND, 00);       

  Intent intent2 = new Intent(context, AlarmReceiver.class);
  PendingIntent Alarm1 = PendingIntent.getBroadcast(context, 0, intent2, 0);

  AlarmManager alarmManager = (AlarmManager) context.getSystemService (Context.ALARM_SERVICE);
  if (cancel)
    alarmManager.cancel (Alarm1);
  else
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, Alarm1);
}

AutoStart像这样调用它onReceive()

@Override
public void onReceive (Context context, Intent intent)
{
  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);     
  makeAlarm (context, prefs.getBoolean("checkboxPref", true));
}

现在,您的 Preference 必须是 some 的一部分Activity。为简单起见,我们将处理该 Activity 中的警报onPause()

@Override
protected void onPause()
{
  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  boolean alarmEnabled = prefs.getBoolean("checkboxPref", true);
  AutoStart.modifyAlarm (this, alarmEnabled);
  super.onPause();
}

现在,当用户取消您的警报或重新启用它时,您的代码会处理这些问题。如果你在 PendingIntent 上调用 cancel,如果它有效,它将取消它,否则不做任何事情,所以我们不必担心检查警报是否已经存在。

于 2012-12-31T20:09:45.320 回答
0
<receiver android:name=".AutoStart">
        <intent-filter>
           <action android:name="android.intent.action.BOOT_COMPLETED" />
           <category android:name="android.intent.category.HOME" />
        </intent-filter>
</receiver>

更新:

SharedPreferences yourPreferences = getSharedPreferences("prefName", MODE_PRIVATE);
if (!yourPreferences.getBoolean("prefName", false)) {
        Editor editor = yourPreferences.edit();
        editor.clear();
        // add your logic here
        //editor.putBoolean("prefName", false);
        //editor.putInt("userId", -1);
        editor.commit();
 }

然后使用类似这样的代码

userId = yourPreferences.getBoolean("userId", -1);
于 2012-12-31T19:10:10.217 回答