我有一段代码,它安排任务每隔10(3,5,20,60,无关紧要)秒重复这个类:
AlarmManager + BroadcastReceiver + IntentService
在SDK模拟器(4.2.2)上,它可以正常工作
07-12 09:02:32.257: INFO/Alarm Receiver(1827): Entered
07-12 09:02:42.256: INFO/Alarm Receiver(1827): Entered
07-12 09:02:52.275: INFO/Alarm Receiver(1827): Entered
但是在我的手机上Xiaomi MI-2S (4.1.1)
它只会每5分钟发光一次
07-12 16:06:07.044: INFO/Alarm Receiver(5632): Entered
07-12 16:11:06.874: INFO/Alarm Receiver(5632): Entered
07-12 16:16:06.864: INFO/Alarm Receiver(5632): Entered
有什么问题?
UPD。这是代码
活动
public void startSchedule
{
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), MyReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10*1000, pIntent);
}
public void endSchedule
{
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), MyReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pIntent);
}
广播接收器
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("Alarm Receiver", "Entered");
context.startService(inService);
}
IntentService 现在为空
@Override
protected void onHandleIntent(Intent arg0)
{
// Do some task
}
答案 0 :(得分:0)
这是小米的v5固件的错误! 我找到了一个解决方法来使我的应用程序工作,但我认为醒来会有问题。我用
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 5 * 1000, pendingIntent);
而不是
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 5 * 1000, pendingIntent);