我有一个在所有Android版本中触发一次的alarmManager,但仅在版本4.4.2中触发两次。 我看到这个版本有变化影响了AlarmManager,但我不知道如何解决它,如果是这样的情况...... 有什么建议吗?
这是代码:
public void setOneTimeTimer(Context context, int id)
{
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
if ((wakelock==null) | ((wakelock != null) && (wakelock.isHeld() == false))){
wl.acquire();
wakelock = wl;
//Release wakeLock is in the Alarm Class
}
AlarmManager am = (AlarmManager)NoteActivity.this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(NoteActivity.this, Alarm.class);
intent.putExtra("name", name);
intent.putExtra("phone", phone);
intent.putExtra("timeSet", timeSet);
PendingIntent pi = PendingIntent.getBroadcast(NoteActivity.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.KITKAT) {
setAlarm(am, interval, pi);
}
else
{
setAlarmToKitkat(am, interval, pi);
}
}
private void setAlarm(AlarmManager am, long interval, PendingIntent pi){
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, pi);
}
@TargetApi(19)
private void setAlarmToKitkat(AlarmManager am, long interval, PendingIntent pi){
am.setWindow(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, 20000, pi);
}
答案 0 :(得分:0)
所以我会在这里写下我的解决方案,以防将来帮助某人: 问题是我将警报设置为TimePickerDialog的“onTimeSet”方法,并且出于某种原因,仅在KitKat版本上它激活警报两次。 当我添加一个空按钮的空(!)方法时,它只计算了一次并激活了警报方法......这就是我添加的方法:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
timePicker.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.title_ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
}
}
});
}