0

我正在尝试创建一个警报,该警报会在设备重新启动时自行重置。这是正确的方法吗(假设 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)
4

3 回答 3

0

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

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

    <receiver android:name="MyAlarmReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
于 2013-01-28T08:35:57.797 回答
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);

请使用这个 PendingIntent.FLAG_UPDATE_CURRENT

于 2013-01-28T08:39:28.860 回答
0

你的上下文是错误的。传递正确的上下文,

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

改成

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

于 2013-01-28T08:39:53.580 回答