1

I've some problem with Oreo and Broadcast Receiver on Boot. My method work for earlier version of Android, but with Oreo (Huawei P20 Lite with Pie) it doesn't work.

The Log.e in the Boot Receiver is not displayed in the LogCat and the action in BootService onCreate is not executed (No Log in log cat).

Boot Receiver

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "Boot Receiver : Boot Start Service : AlarmNotification");

        if ( Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) ) {
            Intent myIntent = new Intent(context, BootService.class);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(myIntent);
            } else {
                context.startService(myIntent);
            }
        }
    }
}

I've set the permissions in the Manifest file

AndroidManifest.xml

        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

        <receiver android:name=".BootReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>
        <service
            android:name=".BootService"
            android:enabled="true"
            android:exported="true">
        </service>

BootService The Boot Service works on earlier versions of Android

public class BootService extends Service {
    public BootService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //Is fired by the Boot Receiver... on Boot !
        Context context = this;
        Log.e(TAG, "Start Alarm");
        //Do something with the code 
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    } 
}

Perhaps I forget something, or do I something wrong ? Can somebody help me on this case, I already tested multiple solutions from StackeOverflow and read the doc, but for me it's not very clear for this case.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lord St John
  • 124
  • 1
  • 2
  • 13
  • 1
    Check my answer here (https://stackoverflow.com/questions/48789572/oreo-broadcastreceiver-sms-received-not-working/52016035#52016035). – shashank chandak Jul 18 '19 at 15:06
  • Thank's @shashankchandak, The work arround of rahul-chowdhury at https://stackoverflow.com/questions/41524459/broadcast-receiver-not-working-after-device-reboot-in-android worked for me ! – Lord St John Jul 23 '19 at 10:32

0 Answers0