Android AlarmManager和通知不工作
关于这个话题,我已经读了好几个问题,但我还是不明白 我需要同时显示一个通知。我正在使用AlarmManager。它看起来写得很好,但实际上我不会收到任何警报。 SyncAlarmReceiver从不调用onReceive() 以下是设置警报的代码:Android AlarmManager和通知不工作,android,alarmmanager,android-notifications,Android,Alarmmanager,Android Notifications,关于这个话题,我已经读了好几个问题,但我还是不明白 我需要同时显示一个通知。我正在使用AlarmManager。它看起来写得很好,但实际上我不会收到任何警报。 SyncAlarmReceiver从不调用onReceive() 以下是设置警报的代码: private static final int ORA_NOTIFICA = 14; //Hour for the notification private static final int MINUTO_NOTIFICA = 35; //M
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>
RTC_WAKEUP和appeased之所以出现在这里,是因为我尝试了使用这两种方法(并没有真正理解其中的区别)
编辑:
我试过阅读用于设置时间的日历,这是它的toString()的结果:
使用在线毫秒时间转换器,时间参数将转换为Wed Apr 08 2409 06:45:10 GMT+0200(西欧夏令时)
我应该用什么来设定时间 问题在于在以下情况下的运行时间:
am.setRepeating(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, getPendingIntentSync(ctx));
更改为RTC后,它工作了。使用
adb shell dumpsys alarm
来确认您的警报是否已设置,以及下一步何时调用。这真是一团糟。是否有方法仅从我的软件包中筛选出报警?使用fgrep
、grep
或等效工具搜索您的软件包名称。我已将我能找到的内容添加到问题中。我已编辑了原始问题,包括我能读到的有关用于设置报警的日历对象的内容。
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]
am.setRepeating(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, getPendingIntentSync(ctx));