Android 一加 6(Android 派版)BOOT COMPLETE 广播接收器不工作

Fer*_*qui 3 android

Android 一加 6(Android 派版)BOOT COMPLETE 广播接收器不工作

 <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>
Run Code Online (Sandbox Code Playgroud)

下面是我的接收器类:

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));
 }
}
Run Code Online (Sandbox Code Playgroud)

一旦启动完成,我需要启动我的服务。请建议我该怎么做

下面是我得到的错误日志

 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)
Run Code Online (Sandbox Code Playgroud)

小智 5

我在 OnePlus 6 中遇到了同样的问题。我要制作后台服务,它应该在重新启动设备后自动启动。大多数设备收到 BOOT_COMPLETED 意图,但在 OnePlus 6 中不起作用。我尝试搜索并找到解决方案。原因是我的应用程序针对电池优化进行了优化。我将状态更改为Not Optimized,之后它运行良好。

在 AndroidManifest.xml 上添加权限

<uses-permission  android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Run Code Online (Sandbox Code Playgroud)

白名单/优化启用了您的应用程序

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我希望它会有所帮助。