报警管理器不会在后台运行在Android 6.0 [英] Alarm Manager does not work in background on Android 6.0
问题描述
这是我的活动
code,
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"/>
这是我的广播接收器
code,
@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();
}
我在清单注册我的广播接收器
,
<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>
以上code成功地在后台执行的广播接收器
上pre-棉花糖设备,但棉花糖设备上,在广播接收器
没有得到执行。有谁知道什么可以发生在这里?谢谢你。
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.
推荐答案
有几件事情你可以尝试这在演唱会中使用时,的应的能够通过削减所有闲置/待机/休眠模式(不论OS版本)。
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 使用<一个href=\"http://developer.android.com/intl/ja/reference/android/support/v4/content/WakefulBroadcastReceiver.html\"相对=nofollow> WakefulBroadcastReceiver
,而不是一个普通的广播接收器
。请确保您包括 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安培;以上)来安排意图
的接收器:
if(Build.VERSION.SDK_INT < 23){
setExact(...)
}
else{
setExactAndAllowWhileIdle(...)
}
参考文献:
1 作为后台服务经理报警 。
2 作为后台工作的流程图,报警和你的Android应用 。
这篇关于报警管理器不会在后台运行在Android 6.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!