AlarmManager没有停止或崩溃

时间:2014-07-03 22:54:30

标签: android android-intent alarmmanager

我试图弄清楚AlarmManager是如何工作的,我已经使这个应用程序重复发出警报,但行为不正常。运行一次后,尝试检查应用程序崩溃,或尝试取消它不再有效。我做错了什么?

public class SampleAlarmReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
private Intent intentxx;

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

    Intent service = new Intent(context, SampleSchedulingService.class);

    startWakefulService(context, service);
}

public void setAlarm(Context context) {
    alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    intentxx = new Intent(context, SampleAlarmReceiver.class);
    intentxx.putExtra("timer", "timer1");
    alarmIntent = PendingIntent.getBroadcast(context, 0, intentxx, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    calendar.add(Calendar.MINUTE, 1);

    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 2, alarmIntent);

    ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

public void cancelAlarm(Context context) {
    if (alarmMgr != null) {
        alarmMgr.cancel(alarmIntent);
        alarmIntent.cancel();
    }

    ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

public boolean checkAlarm(Context context) {
    boolean alarmUp = (PendingIntent.getBroadcast(context, 0, intentxx, PendingIntent.FLAG_NO_CREATE) != null);

    return alarmUp;
}

编辑: 在它触发一次后,我尝试检查()并且崩溃发出此错误。正如我所说,我甚至不能取消它()它,它一直在继续运行。

07-04 01:19:20.244: E/AndroidRuntime(29863): FATAL EXCEPTION: main
07-04 01:19:20.244: E/AndroidRuntime(29863): java.lang.NullPointerException
07-04 01:19:20.244: E/AndroidRuntime(29863):    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:487)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at android.app.PendingIntent.getBroadcast(PendingIntent.java:472)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.example.android.scheduler.SampleAlarmReceiver.checkAlarm(SampleAlarmReceiver.java:143)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.example.android.scheduler.MainActivity.onOptionsItemSelected(MainActivity.java:61)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at android.app.Activity.onMenuItemSelected(Activity.java:2640)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1171)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at android.view.View.performClick(View.java:4475)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at android.view.View$PerformClick.run(View.java:18786)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at android.os.Handler.handleCallback(Handler.java:730)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at android.os.Looper.loop(Looper.java:137)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at android.app.ActivityThread.main(ActivityThread.java:5419)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at java.lang.reflect.Method.invokeNative(Native Method)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at java.lang.reflect.Method.invoke(Method.java:525)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
07-04 01:19:20.244: E/AndroidRuntime(29863):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

看起来这个版本可行。结果是每次都必须创建pendingIntent。

public class SampleAlarmReceiver extends WakefulBroadcastReceiver {
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, SampleSchedulingService.class);

        startWakefulService(context, service);
    }

    public void setAlarm(Context context) {
        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, SampleAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.MINUTE, 1);

        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 1, alarmIntent);

        ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }

    public void cancelAlarm(Context context) {
        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, SampleAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        alarmMgr.cancel(alarmIntent);
        alarmIntent.cancel();

        ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }

    public boolean checkAlarm(Context context) {
        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, SampleAlarmReceiver.class);
        boolean alarmUp = (PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE) != null);

        return alarmUp;
    }
}