我目前正在开发一个提醒应用程序,到目前为止我已经设置了 2 个闹钟:一个每天都会响起(AlarmX),如果它是正确的日期,则设置另一个闹钟(Alarm)来唤醒屏幕。
这段代码在我的脑海中是有道理的,但它没有执行我想要的,并且 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 相同,但根据日志 cat 得到错误,即使它没有被调用。
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();
}
现在据我所知,即使我没有调用警报,并且由于我没有将包传递给它而创建了一个空指针异常,但AlarmX
在am.setRepeating()
命令中触发警报方法时也是如此。这是输出...onReceive()
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>