Android One plus 6(Android pie version) BOOT COMPLETE Broadcast Receiver not working
<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>
below is my reciever class :
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));
}
}
I need to start my service once boot get completed. Please suggest what should i do
below is error log i am getting
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)
I faced same problem in OnePlus 6. I was going to make background service and it should be auto-started after restarting device. The BOOT_COMPLETED intent received for most devices, but not working in OnePlus 6. I tried to search and found solution. The reason is that my app is optimized on Battery Optimization. I changed the status to Not Optimized, after that it's working well.
Add permission on AndroidManifest.xml
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
White-list / optimization enabled your app
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);
}
}
I hope it will be helpful.
This is a special broadcast which you must hold a specific permission in order to receive (from the documentation for ACTION_BOOT_COMPLETED
):
You must hold the Manifest.permission.RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.
Be sure to declare that in your manifest. No runtime permissions are needed since it is a "normal" level permission. However, your app must also get started for the first time by the user (e.g. an Activity
in your app) before the system will ever deliver this broadcast to your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With