AlarmManager从不在AlarmReceiver / BroadcastReceiver中调用onReceive [英] AlarmManager never calling onReceive in AlarmReceiver/BroadcastReceiver
问题描述
我仍然无法触发我的AlarmReceiver类的onReceive方法。
I still cannot get my AlarmReceiver class' onReceive method to fire. Does anything stick out as wrong with this implementation?
所有要做的事情是等待一段时间(最好是6天),然后弹出一个通知。 (您可以相信没有内置的系统吗?crontab有人吗??)
All this is supposed to do is wait a certain period of time (preferably 6 days) and then pop up a notification. (Can you believe there isn't a built in system for this? crontab anyone!?)
MyActivity和BootReceiver都在必要条件下设置了警报。 AlarmService发出通知。而AlarmReceiver是应该来捕获警报并启动AlarmService,但是它从未捕获到该广播,并且无论我做什么都不会。
MyActivity and BootReceiver both set up an alarm under the necessary conditions. AlarmService kicks out a notification. And AlarmReceiver is supposed to catch the alarm and kick off AlarmService, but it has never caught that broadcast, and won't no matter what I do.
哦,我正在Droid X 2.3.4上进行测试。正在根据API 8构建项目。
Oh, and I've been testing on my Droid X, 2.3.4. Project being built against API 8.
P.S。大部分内容已从 http:// android-in- Practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/
P.S. Most of this has been adapted from http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/
------------ MyActivity.java- ----------
------------ MyActivity.java ------------
public class MyActivity extends Activity implements SensorEventListener {
private void setupAlarm() {
Log.i(TAG, "Setting up alarm...");
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, new Intent(context, AlarmReceiver.class), 0);
// Get alarm trigger time from prefs
Log.i(TAG, "Getting alarm trigger time from prefs...");
SharedPreferences mPrefs2 = PreferenceManager.getDefaultSharedPreferences(context);
long trigger = SocUtil.getLongFromPrefs(mPrefs2, AlarmConst.PREFS_TRIGGER);
Log.i(TAG, "Trigger from prefs: " + trigger + " (" + new Date(trigger).toString() + ").");
// If alarm trigger is not set
if(trigger == new Long(-1).longValue()) {
// Set it
trigger = new Date().getTime() + NOTIFY_DELAY_MILLIS;
SocUtil.saveLongToPrefs(mPrefs2, AlarmConst.PREFS_TRIGGER, trigger);
Log.i(TAG, "Trigger changed to: " + trigger + " (" + new Date(trigger).toString() + ").");
// And schedule the alarm
alarmMgr.set(AlarmManager.RTC, trigger, pendingIntent);
Log.i(TAG, "Alarm scheduled.");
}
// If it is already set
else {
// Nothing to schedule. BootReceiver takes care of rescheduling it after a reboot
}
}
}
------------ AlarmService.java ------------
------------ AlarmService.java ------------
public class AlarmService extends IntentService {
public AlarmService() {
super("AlarmService");
}
@Override
public void onHandleIntent(Intent intent) {
Log.i(AlarmConst.TAG, "AlarmService invoked.");
this.sendNotification(this);
}
private void sendNotification(Context context) {
Log.i(AlarmConst.TAG, "Sending notification...");
Intent notificationIntent = new Intent(context, Splash.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "Test1", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Test2", "Test3", contentIntent);
notificationMgr.notify(0, notification);
}
}
----------- -AlarmReceiver.java ------------
------------ AlarmReceiver.java ------------
public class AlarmReceiver extends BroadcastReceiver {
// onReceive must be very quick and not block, so it just fires up a Service
@Override
public void onReceive(Context context, Intent intent) {
Log.i(AlarmConst.TAG, "AlarmReceiver invoked, starting AlarmService in background.");
context.startService(new Intent(context, AlarmService.class));
}
}
----------- -BootReceiver.java ------------
(还原擦除的警报,因为我在操作系统中安排的工作不足以通过重新引导-_-停留在此位置)
------------ BootReceiver.java ------------ (to restore wiped alarms, because stuff I schedule with the OS isn't important enough to stick around through a reboot -_-)
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(AlarmConst.TAG, "BootReceiver invoked, configuring AlarmManager...");
Log.i(AlarmConst.TAG, "Setting up alarm...");
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, new Intent(context, AlarmReceiver.class), 0);
// Get alarm trigger time from prefs
Log.i(AlarmConst.TAG, "Getting alarm trigger time from prefs...");
SharedPreferences mPrefs2 = PreferenceManager.getDefaultSharedPreferences(context);
long trigger = SocUtil.getLongFromPrefs(mPrefs2, AlarmConst.PREFS_TRIGGER);
Log.i(AlarmConst.TAG, "Trigger from prefs: " + trigger + " (" + new Date(trigger).toString() + ").");
// If trigger exists in prefs
if(trigger != new Long(-1).longValue()) {
alarmMgr.set(AlarmManager.RTC, trigger, pendingIntent);
Log.i(AlarmConst.TAG, "Alarm scheduled.");
}
}
}
------ ------清单------------
------------ Manifest ------------
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
</activity>
<receiver android:name="com.domain.app.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name="com.domain.app.AlarmReceiver"></receiver>
<service android:name="com.domain.app.AlarmService"></service>
推荐答案
我什至不使用<$ c来解决了这个问题$ c> BroadcastReceiver 。我读过的有关如何进行通知警报的教程和帖子中的每一个,都说使用 BroadcastReceiver
,但是显然我不明白
I solved this by not even using a BroadcastReceiver
. Every single one of the tutorials and posts I read about how to do notification alarms (and that was A LOT) said to use a BroadcastReceiver
, but apparently I don't understand something, or that's a load of crap.
现在,我只是将 AlarmManager
设置为带有<$ c的警报$ c>意图直接进入我创建的新的活动
。在重新启动后,我仍然使用 BootReceiver
重置该警报。
Now I just have the AlarmManager
set an alarm with an Intent
that goes directly to a new Activity
I created. I still use the BootReceiver
to reset that alarm after a reboot.
这可以使通知在应用程序中运行,退出应用程序,应用程序进程被终止,并在重新启动后。
This lets the notification work in-app, out-of-app, with the app process killed, and after a reboot.
感谢您的宝贵时间。
这篇关于AlarmManager从不在AlarmReceiver / BroadcastReceiver中调用onReceive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!