我有一段代码,它安排任务每隔 10 秒(3,5,20,60,没关系)重复这个类:
AlarmManager + BroadcastReceiver + IntentService
在 SDK Emulator (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
有什么问题?
更新。这是代码
活动
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
}