[英]AlarmManager and Notification not working
我已經閱讀了有關此主題的幾個問題,但仍然無法弄清楚。
我需要每天同時顯示一條通知。 我正在使用AlarmManager。 看起來寫得不錯,但實際上我不會收到任何警報。 SyncAlarmReceiver永遠不會調用onReceive()。
這是設置警報的代碼:
private static final int ORA_NOTIFICA = 14; //Hour for the notification
private static final int MINUTO_NOTIFICA = 35; //Minutes for the notification
private static final int ID_NOTIFICA_SYNC = 33; //Notification's ID
public static void setSyncAlarm(Context ctx) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, ORA_NOTIFICA);
calendar.set(Calendar.MINUTE, MINUTO_NOTIFICA);
AlarmManager am = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, getPendingIntentSync(ctx));
Log.d("NOTIFICA SYNC", "Alarm set at " + ORA_NOTIFICA + ":"
+ MINUTO_NOTIFICA);
}
public static PendingIntent getPendingIntentSync(Context ctx) {
return PendingIntent.getBroadcast(ctx, 0, new Intent(ctx,
SyncAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
}
這是SyncAlarmReceiver:
public class SyncAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("NOTIFICA SYNC", "Alert Ricevuto");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
NotificationUtils.createNotifSync(context);
wl.release();
}
}
最后是createNotifSync:
public static void creaNotificaSync(Context ctx) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setSmallIcon(R.drawable.action_bar_icon)
.setContentTitle(ctx.getString(R.string.sync_notif_titolo))
.setContentText(ctx.getString(R.string.sync_notif_testo))
.setAutoCancel(true);
Intent resultIntent = new Intent(ctx, ActTestJson.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
stackBuilder.addParentStack(ActTestJson.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(ID_NOTIFICA_SYNC, mBuilder.build());
}
另外,清單中的代碼段:
<receiver
android:name="com.mypackage.SyncAlarmReceiver"
android:enabled="true" >
</receiver>
編輯:這就是我在dumbsys警報上使用findstr所發現的
adb shell dumpsys alarm | findstr /C:com.mypackage
RTC_WAKEUP #33: Alarm{42bd9470 type 0 com.mypackage} operation=PendingIntent{429d3238: PendingIntentRecord42fe80a0 com.mypackage startService}}
ELAPSED #12: Alarm{42b4fcc8 type 3 com.mypackage} operation=PendingIntent{42914e20: PendingIntentRecord{42a2a198 com.mypackage broadcastIntent}}
com.contram.unicam.controllori +15ms running, 6 wakeups: +15ms 6 wakes 6 alarms: cmp={com.mypackage/com.mypackage.utils.SyncAlarmReceiver}
RTC_WAKEUP和ELAPSED在這里是因為我嘗試使用這兩種方法(並不真正了解它們的區別)。
編輯:我試圖讀取用於設置時間的日歷,這就是它的toString()的結果:
java.util.GregorianCalendar[time=1386194671008,areFieldsSet=true,lenient=true,
zone=Europe/Rome,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2013,MONTH=11,
WEEK_OF_YEAR=49,WEEK_OF_MONTH=2,DAY_OF_MONTH=4,DAY_OF_YEAR=338,DAY_OF_WEEK=4,
DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=4,SECOND=31,MILLISECOND=8,
ZONE_OFFSET=3600000,DST_OFFSET=0]
使用在線千禧時間轉換器,時間參數轉換為格林尼治標准時間+0200(W. Apr Day 2 2 08)(W. Europe Daylight Time)。
我應該用什么來設置時間?
問題出在ELAPSED_TIME:
am.setRepeating(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, getPendingIntentSync(ctx));
更改為RTC,它起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.