重启时AlarmManager

时间:2013-01-28 08:32:52

标签: android startup alarmmanager

我尝试创建一个在设备重启时自动重置的闹钟。这是正确的方法吗(假设Android清单是正确的)?代码似乎崩溃了我的应用程序。

public class AlarmReceiver extends BroadcastReceiver {
     Context cxt;

 @Override
 public void onReceive(Context context, Intent intent) {
     cxt = context;
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getDefault());
        updateTime.set(Calendar.HOUR_OF_DAY, 0);
        updateTime.set(Calendar.MINUTE, 0);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234, 
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
    }
}

这是Logcat:

01-28 00:35:00.263: E/AndroidRuntime(31269): FATAL EXCEPTION: main
01-28 00:35:00.263: E/AndroidRuntime(31269): java.lang.RuntimeException: Unable to start receiver com.neelsomani.meds.AlarmReceiver: java.lang.NullPointerException
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2265)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread.access$1600(ActivityThread.java:139)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.os.Looper.loop(Looper.java:137)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread.main(ActivityThread.java:4918)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at java.lang.reflect.Method.invokeNative(Native Method)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at java.lang.reflect.Method.invoke(Method.java:511)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at dalvik.system.NativeStart.main(Native Method)
01-28 00:35:00.263: E/AndroidRuntime(31269): Caused by: java.lang.NullPointerException
01-28 00:35:00.263: E/AndroidRuntime(31269):    at com.neelsomani.meds.AlarmReceiver.onReceive(AlarmReceiver.java:40)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2258)

3 个答案:

答案 0 :(得分:0)

这段代码看起来不错,你能告诉我们崩溃的更多细节吗?

您是否在AndroidManifest.xml中声明了此broadcastReceiver?

    <receiver android:name="MyAlarmReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

答案 1 :(得分:0)

AlarmManager am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent intent1 = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(Calendar.HOUR_OF_DAY, (int) h);
        calendar1.set(Calendar.MINUTE, (int) m);

plz使用此PendingIntent.FLAG_UPDATE_CURRENT

答案 2 :(得分:0)

你的背景错了。传递正确的背景,

AlarmManager am = (AlarmManager) cxt.getSystemService(Context.ALARM_SERVICE);

更改为

  

AlarmManager am =(AlarmManager)   context.getSystemService(Context.ALARM_SERVICE);