![](/img/trans.png)
[英]AlarmManager BroadcastReceiver, onReceive never called
[英]AlarmManager never calling onReceive in AlarmReceiver/BroadcastReceiver
我仍然無法觸發我的AlarmReceiver類'onReceive方法。 這個實現有什么問題嗎?
所有這一切應該是等待一段時間(最好是6天),然后彈出一個通知。 (你能相信沒有內置系統嗎?crontab任何人!?)
MyActivity和BootReceiver都在必要條件下設置了警報。 AlarmService啟動通知。 並且AlarmReceiver 應該捕獲警報並啟動AlarmService,但它從未捕獲過該廣播,並且不管我做什么都不會。
哦,我一直在測試我的Droid X,2.3.4。 項目是針對API 8構建的。
PS大部分內容改編自http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/
------------ 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 ------------
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 ------------
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 ------------(恢復已擦除的警報,因為我在操作系統中安排的內容不夠重要,無法通過重啟 -_-)
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.");
}
}
}
------------清單------------
<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>
以下是我最近用於每小時發出通知的一些代碼(這是我的MainActivity中):
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent Notifyintent = new Intent(context, Notify.class);
PendingIntent Notifysender = PendingIntent.getBroadcast(this, 0, Notifyintent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 3600000, Notifysender);
然后在Notify.java中
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Notify extends BroadcastReceiver{
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager myNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Update Device", 0);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, "Device CheckIn", "Please run Device CheckIn", contentIntent);
notification.flags |= Notification.FLAG_HIGH_PRIORITY;
myNotificationManager.notify(0, notification);
}
}
最后在AndroidManifest.xml中我在標簽之間有這個:
<receiver android:name=".Notify" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
我知道我在辦公室工作的主要代碼,隨時向我發送電子郵件以獲得更多幫助,因為我遇到了同樣的問題。
電子郵件:mit.edu的sbrichards
您必須使用intent Action注冊AlarmReceiver。 如下。 並且動作字符串必須與sendBroadcast()方法廣播的動作相同。
像sendBroadcast(new Intent(""com.intent.action.SOMEACTION.XYZ""));
<receiver android:name="com.domain.app.AlarmReceiver">
<intent-filter>
<action android:name="com.intent.action.SOMEACTION.XYZ" />
</intent-filter>
</receiver>
我甚至沒有使用BroadcastReceiver
解決了這個問題。 我讀到的關於如何進行通知警報(也就是A LOT)的每一篇教程和帖子都說使用了BroadcastReceiver
,但顯然我聽不懂東西,或者說這是一堆垃圾。
現在我讓AlarmManager
設置一個警報,其中一個Intent
直接轉到我創建的新Activity
。 我仍然使用BootReceiver
在重啟后重置該警報。
這使得通知可以在應用程序內部,應用程序外進行,應用程序進程被終止,並且在重新啟動后。
感謝您的其他評論者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.