其他应用程序运行时,AlarmManager间隔会更改

时间:2017-06-06 17:53:49

标签: android alarmmanager intervals android-alarms

我正在处理设置重复闹铃的应用。

例如,它应该每2分钟运行一次我的服务。 当myApplication活动正在运行时,它工作正常,但当其他应用程序在前台运行时,间隔时间为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;

}

1 个答案:

答案 0 :(得分:0)

来自AlarmManager docs

  

注意:从API 19开始,所有重复警报都不准确。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,每次重新安排如上所述。 targetSdkVersion早于API 19的旧应用程序将继续将所有警报(包括重复警报)视为完全警报。

您可能会遇到这些不准确的警报。

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