2017-06-06 40 views
0

我正在设置一个设置重复闹钟的应用程序。当其他应用程序正在运行时AlarmManager时间间隔发生变化

例如,它应该每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; 

} 
+0

你在运行什么android版本? –

+0

lollipop api 22 – Mostafa

回答

0

AlarmManager docs

注:为API 19日,所有重复报警是不准确的。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,如上所述每次重新安排时间。其targetSdkVersion早于API 19的传统应用程序将继续拥有其所有警报,包括重复警报,并将其视为确切对待。

您可能正在经历那些不准确的警报。

另请注意,如果您希望设备在警报熄灭时唤醒,则需要RTC_WAKEUP

相关问题