多次报警死机

时间:2014-12-18 20:28:00

标签: android alarmmanager android-alarms repeatingalarm

我目前正在开发一个提醒应用程序,到目前为止我已经设置了2个警报:一个每天都会关闭(AlarmX),如果它是正确的日期,则设置另一个警报(警报)唤醒屏幕。

这段代码在我脑海中有意义,但它没有执行我想要的,并且logcat显示错误,我不明白他是如何捕获的。

这是代码,也许你看到我不知道的东西。

AlarmX - 扩展BroadcastReceiver

的类
    public void SetAlarm(Context context)
{
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmX.class);
    intent.putExtra(ONE_TIME, Boolean.FALSE);
    intent.setAction("com.todo.list.brodcast.ALARMX");
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    //After after 30 seconds
    Calendar c = Calendar.getInstance();
    Calendar k = Calendar.getInstance();
    k.set(Calendar.PM, 12);
    if(c.getTimeInMillis()-k.getTimeInMillis()>0) k.setTimeInMillis(k.getTimeInMillis()+24*60*60*1000);
    am.setRepeating(AlarmManager.RTC_WAKEUP, k.getTimeInMillis() , 24*60*60*1000 , pi); }

此方法使应用程序崩溃,并且永远不会调用此类的onReceive()方法。 我从Fragment这样称呼它:
AlarmX alarm = new AlarmX(); alarm.SetAlarm(getActivity().getApplicationContext()); 还有Alarm类 - 与AlarmX相同,但根据log cat获取Error,即使它没有被调用。

public void onReceive(Context context, Intent intent) {
    /*acquire power service manager*/
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    @SuppressWarnings("deprecation")
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,"TAG");
    wl.acquire();

    //TODO creaate Notification object
    Bundle b = intent.getBundleExtra("Task");
    UUID id = UUID.fromString(b.getString("UUID"));
    Notification n = new Notification(context, id);
    n.setDescription(b.getString("Description"));
    n.setTitle(b.getString("Topic"));
    n.notifyUser();

    wl.release();
}

现在我理解在AlarmXam.setRepeating()命令在Alarm中触发onReceive()方法,即使我没有调用alarm并且onReceive()创建了一个空指针异常因为我没有通过捆绑。这是logcat输出...

12-18 22:36:22.857: E/AndroidRuntime(30124): FATAL EXCEPTION: main
12-18 22:36:22.857: E/AndroidRuntime(30124): Process: com.todo.list:remote, PID: 30124
12-18 22:36:22.857: E/AndroidRuntime(30124): java.lang.RuntimeException: Unable to start receiver com.todo.list.brodcast.Alarm: java.lang.NullPointerException
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2856)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread.access$1700(ActivityThread.java:156)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.os.Looper.loop(Looper.java:157)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread.main(ActivityThread.java:5872)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at java.lang.reflect.Method.invokeNative(Native Method)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at java.lang.reflect.Method.invoke(Method.java:515)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at dalvik.system.NativeStart.main(Native Method)
12-18 22:36:22.857: E/AndroidRuntime(30124): Caused by: java.lang.NullPointerException
12-18 22:36:22.857: E/AndroidRuntime(30124):    at com.todo.list.brodcast.Alarm.onReceive(Alarm.java:29)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2845)
12-18 22:36:22.857: E/AndroidRuntime(30124):    ... 10 more

谢谢!

编辑 - Manifest.xml 以下是我在Manifest文件中声明接收器的方式:

<receiver
        android:name="com.todo.list.brodcast.Alarm"
        android:exported="false"
        android:process=":remote" >
        <intent-filter>
            <action android:name="com.todo.list.brodcast.ALARM" />
        </intent-filter>
    </receiver>
      <receiver
        android:name="com.todo.list.brodcast.AlarmX"
        android:exported="false"
        android:process=":Asd" >
        <intent-filter>
            <action android:name="com.todo.list.brodcast.ALARMX" />
        </intent-filter>
    </receiver>

1 个答案:

答案 0 :(得分:0)

为什么不实施一个可以关闭而不是调用另一个的警报,只需更换功能?
我认为这将解决您的问题,并简化代码。