当其他应用程序为 运行 时,AlarmManager 间隔会发生变化

AlarmManager interval changes when other application is running

我正在开发一个设置重复闹钟的应用程序。

例如,它应该每 2 分钟 运行 我的一项服务。 当 myApplication activity 正在 运行ning 时它工作正常,但是当其他应用程序在前台 运行ning 时,间隔变为 5 分钟!!!

我 运行 此代码在另一个在前台启动的服务中。

我不知道为什么会这样。

public class Serv1 extends Service{

 public static Intent intentServ2 = new Intent(G.context,Serv2.class);
public static   PendingIntent pendingIntend=PendingIntent.getService(G.context,0,intentServ2,0);
public  static AlarmManager alarmManager;

public int onStartCommand(Intent intent, int flags, int startId) {
 alarmManager =(AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis()+(2*1000),120*1000,pendingIntend);


  Intent notificationIntent = new Intent(this, Serv1.class);

    PendingIntent pendingIntent = PendingIntent.getService(this, 0,
            notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Serive1")
            .setContentText("Service is working...")
            .setContentIntent(pendingIntent).build();


    startForeground(254698, notification);





    return Service.START_STICKY;

}

来自AlarmManager docs

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

您可能遇到了那些不准确的警报。

另请注意,如果您希望设备在闹钟响起时唤醒,则需要RTC_WAKEUP