Android 一加 6(Android 派版) 启动完成 广播接收器不工作
<receiver
android:name=".service.ConnectionReceiver"
android:enabled="true" android:exported="true"
>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.conn.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
下面是我的接收器类:
public class ConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("API123", "" + intent.getAction());
Toast.makeText(context, "isatRunningonbootanyproblem", Toast.LENGTH_SHORT).show();
context.startService(newIntent(context, MyBackgroundService.class));
}
}
启动完成后我需要启动我的服务。请建议我应该做什么
下面是我收到的错误日志
018-11-06 05:25:34.994 885-3000/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=oneplus.intent.action.ANY_DATA_STATE flg=0x10 } to test.myapplication/.MyReceiver
2018-11-06 05:25:38.241 885-3000/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.net.conn.DATA_ACTIVITY_CHANGE flg=0x10 (has extras) } to test.myapplication/.MyReceiver requires android.permission.RECEIVE_DATA_ACTIVITY_CHANGE due to sender android (uid 1000)
我在 OnePlus 6 中遇到了同样的问题。我打算进行后台服务,它应该在重新启动设备后自动启动。
大多数设备都会收到 BOOT_COMPLETED 意图,但在 OnePlus 6 中不起作用。
我尝试搜索并找到了解决方案。原因是我的应用程序针对电池优化进行了优化。
我将状态更改为未优化,之后就运行良好了。
在AndroidManifest.xml上添加权限
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
白名单/优化启用您的应用
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivityForResult(intent, 1000);
}
}
我希望这会有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)