0

我有一段代码,它安排任务每隔 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
}
4

1 回答 1

0

是小米v5固件的bug!我找到了一种解决方法来使我的应用程序正常工作,但我认为唤醒会有问题。我用

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 5 * 1000, pendingIntent);

代替

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 5 * 1000, pendingIntent);
于 2013-08-06T09:40:19.437 回答