我已经阅读了有关此主题的几个问题,但我仍然无法弄清楚。
我需要每天在同一时间显示通知。我正在使用警报管理器。看起来写得很好,但我实际上不会收到任何警报。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]
使用在线毫秒到时间转换器,时间参数转换为 Wed Apr 08 2409 06:45:10 GMT+0200(W. Europe Daylight Time)。
我应该用什么来设置时间?