警报管理器在 Android 6.0 上无法在后台运行 [英] Alarm Manager does not work in background on Android 6.0

查看:41
本文介绍了警报管理器在 Android 6.0 上无法在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Activity代码,

Long time = new GregorianCalendar().getTimeInMillis()+20000;//Setting alarm after 20 sec
Intent intentAlarm = new Intent("alarm");
intentAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentAlarm.putExtra("req_code",10);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,10, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

这些是我在我的应用程序中拥有的所有权限,

These are all the permissions that I have in my app,

  <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="com.myapp.pack.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

这是我的BroadcastReceiver代码,

@Override
public void onReceive(Context context, Intent intent) {
        SharedPreferences sharedPreferences =
        context.getSharedPreferences( "mydata", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("elligible",true);
        editor.apply();

    }

我已经在清单中注册了我的 BroadcastReceiver

I have registered my BroadcastReceiver in the manifest,

 <receiver android:name="com.myapp.pack.AlarmReciever" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="alarm" />
        </intent-filter>
    </receiver>

上面的代码在后台成功地在 MarshMallow 之前的设备上执行了 BroadcastReceiver,但是在 MarshMallow 设备上,BroadcastReceiver 没有被执行.有谁知道这里会发生什么?谢谢.

The above code successfully executes the BroadcastReceiver on pre-MarshMallow devices in the background ,but on a MarshMallow device ,the BroadcastReceiver does not get executed. Does anyone know what could be happening here? Thanks.

推荐答案

你可以尝试一些事情,当它们一起使用时,应该能够解决所有的空闲/standby/doze 模式(与操作系统版本无关).

There are a couple of things you can try which, when used in concert, should be able to cut through all of the idle/standby/doze modes (irrespective of OS version).

1. 使用 WakefulBroadcastReceiver 而不是普通的 BroadcastReceiver.确保包含 WAKE_LOCK 正确使用它的许可.

1. Use a WakefulBroadcastReceiver instead of an ordinary BroadcastReceiver. Make sure you include the WAKE_LOCK permission to use it correctly.

2. 使用 setExactAndAllowWhileIdle() 方法(在 API 23 及以上)用于安排 Intent接收方:

if(Build.VERSION.SDK_INT < 23){
    if(Build.VERSION.SDK_INT >= 19){
        setExact(...);
    }
    else{
        set(...);
    }
}
else{
    setExactAndAllowWhileIdle(...);
}

参考:

1. 后台服务的报警管理器.

2. 后台工作流程图、警报,以及您的 Android 应用.

这篇关于警报管理器在 Android 6.0 上无法在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆