这是我用来为我的小部件设置警报的代码:
private static void setAlarm(Context context) {
Intent myIntent = new Intent(context, Widget.class);
myIntent.setAction(AUTO_UPDATE);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 8);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 8000,
pendingIntent);
}
但问题是即使在睡眠模式下,onReceive()
仍然是由意图触发的。
虽然在使用setInexactRepeating
instead of之后setRepeating
,在睡眠模式下通话之间的延迟会增加到 1 分钟,但这仍然是耗电的。