无论何时重启设备并重置警报,应用程序都会停止工作错误

时间:2014-08-11 19:22:44

标签: android

我正在创建一个包含闹钟的Android应用。无论何时重启设备,我都想再次重置警报。为此,我使用广播接收器接收启动完成的操作,然后重置警报。但是,当我运行应用程序然后重新启动设备时,我收到一条错误消息,指出我的应用程序已停止工作。

谁能告诉我为什么会这样?感谢

广播接收器代码:

public class BootReceiver extends BroadcastReceiver
{

   @Override
   public void onReceive(Context context, Intent intent)
   {

      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.HOUR_OF_DAY,21);
      cal.set(Calendar.MINUTE,50);

      if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED"))
      {
         Toast.makeText(context, "Alarm has been reset for " + cal.getTime().toString(), Toast.LENGTH_SHORT).show();
         // Set the alarm here.
         Intent resetAlarm = new Intent(context, AlarmReceiver.class);
         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234, resetAlarm, 0);
         AlarmManager alarm = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
         alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2 * 60 * 1000, pendingIntent);
      }
   }
}

Android清单文件:

        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

        <receiver android:name=".BootReceiver" android:enabled="true" android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
        </receiver>

0 个答案:

没有答案