Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 用于后台服务的报警管理器_Android_Android Service_Alarmmanager_Android Alarms_Android 6.0 Marshmallow - Fatal编程技术网

Android 用于后台服务的报警管理器

Android 用于后台服务的报警管理器,android,android-service,alarmmanager,android-alarms,android-6.0-marshmallow,Android,Android Service,Alarmmanager,Android Alarms,Android 6.0 Marshmallow,我已经实现了报警管理器,每隔15分钟定期唤醒后台服务。它工作得很好,但是自从加入了打瞌睡模式的安卓6.0之后,它的行为似乎很奇怪,每15分钟都不会醒来。虽然我使用的是alarm.setExactAndAllowHileIDLE()方法,但仍不能在空闲状态下工作 下面是我实现报警管理器的方法 private void serviceRunningBackground() { final Intent restartIntent = new Intent(this, service.cla

我已经实现了报警管理器,每隔15分钟定期唤醒后台服务。它工作得很好,但是自从加入了打瞌睡模式的安卓6.0之后,它的行为似乎很奇怪,每15分钟都不会醒来。虽然我使用的是alarm.setExactAndAllowHileIDLE()方法,但仍不能在空闲状态下工作

下面是我实现报警管理器的方法

 private void serviceRunningBackground()
{
    final Intent restartIntent = new Intent(this, service.class);
    restartIntent.putExtra("ALARM_RESTART_SERVICE_DIED", true);
    alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
   Handler restartServiceHandler;
    restartServiceHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
             pintent = PendingIntent.getService(getApplicationContext(), 0, restartIntent, 0);
             if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
                 Log.d(TAG, " Marshmellow "+ TIMER_START_TIME);
                 alarmMgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, 900000, pintent);                  
             } else {
                 alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 900000, pintent);
             }
             sendEmptyMessageDelayed(0, TIMER_START_TIME);
         }
     };
    restartServiceHandler.sendEmptyMessageDelayed(0, 0);
}
如果您有任何帮助,我们将不胜感激。谢谢您尝试以下方法:

public class PollReceiver extends WakefulBroadcastReceiver{
    static final String PERIOD = "period";
    @Override
    public void onReceive(Context context, Intent intent){
         startWakefulService(context,new Intent(context,MyService.class));
         long period = intent.getLongExtra(PERIOD,-1);
         if(period>0){
         scheduleExactAlarm(context,(AlarmManager)context.getSystemService(Context.ALARM_SERVICE),period)
         }
    }


    static void scheduleExactAlarm(Context context,AlarmManager alarms, long period){
    Intent i = new Intent(context,PollReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context,0,i,0);
    if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1){
        alarms.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime() + period,pi);
    }
}

我在Doze中用这种方法测试了定时警报,它可以正常工作。每15分钟一次。检查一下,这就是我发现这种安排重复警报的方法的地方。

您现在正在测试哪个版本?Nexus 5上的Android 6.0预览版,使用
System.currentTimeMillis()+900000
,它将在15分钟后触发警报。您需要在
service.java的初始方法中再次编写这样的代码,这样它将在接下来的15分钟内设置警报。我会检查一下,只是一个奇怪的问题,因为他们没有提供setRepeating()之类的方法…如果我错了,请纠正我。他们正在努力提高电池寿命,甚至自API 19或更高版本以来,
setRepeating
也不再在精确时间触发警报。有关此
http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,long,long,android.app.pendingent)