0
我已閱讀了有關此主題的幾個問題,但我仍然無法弄清楚。AlarmManager和通知不起作用
我需要每天同時顯示通知。我正在使用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());
}
而且,th E從清單片段:
<receiver
android:name="com.mypackage.SyncAlarmReceiver"
android:enabled="true" >
</receiver>
編輯:這就是我發現使用FINDSTR在dumbsys報警
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和已在這裏是因爲我試圖用兩種方法(沒有真正理解的差異) 。
編輯: 我試圖讀取用於設置時間的日曆,這是什麼出來它的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]
使用在線米利斯到時間轉換器,時間參數轉換爲Wed Apr 08 2409 06:45:10 GMT + 0200(W. Europe Daylight Time)。
我應該用什麼來設置時間?
使用**'adb shell dumpsys alarm' **來確認您的鬧鐘正在設置以及下次被調用的時間。 – CommonsWare
這是一個巨大的混亂。有沒有辦法只濾出我包裝上的警報? –
使用**'fgrep' **,**'grep' **或等價物搜索您的軟件包名稱。 – CommonsWare