1. Download our Official Android App: Forums for Android!

Android not receive BOOT_COMPLETED broadcast

Discussion in 'Android Devices' started by Cuizhe, Oct 13, 2017.

  1. Cuizhe

    Cuizhe Lurker
    Thread Starter
    Rank:
    None
    Points:
    6
    Posts:
    1
    Joined:
    Oct 13, 2017

    Oct 13, 2017
    1
    1
    6
    I'm going to start a service when device boot completed. I write my code following this:

    Manifest.xml

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <receiver android:name="com.test.MBootBroadcastReceiver" android:enabled="true" android:exported="false" >
    <intent-filter android:priority="1">
    <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
    </receiver>
    <service android:name="com.test.TestService" android:enabled="true" android:exported="true" />

    MBootBroadcastReceiver.java

    public class MBootBroadcastReceiver extends BroadcastReceiver {
    @override
    public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "BOOT_COMPLETED:" + intent.getAction(), Toast.LENGTH_LONG).show();
    Intent i = new Intent(context, TestService.class);
    context.startService(i);
    }
    }

    TestService.java

    public class TestService extends Service {
    @nullable
    @override
    public IBinder onBind(Intent intent) {
    return null;
    }
    @override
    public int onStartCommand(Intent intent, int flags, int startId) {
    Notification notification = new NotificationCompat.Builder(this)
    .setContentTitle(getString(R.string.app_name))
    .setContentText("Test Service Running")
    .setSmallIcon(R.mipmap.ic_launcher)
    .setWhen(System.currentTimeMillis())
    .setPriority(Notification.PRIORITY_MIN)
    .setOngoing(true)
    .build();
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    startForeground(startId, notification);
    return super.onStartCommand(intent, flags, startId);
    }
    }

    It works good when I run adb shell am broadcast -a android.intent.action.BOOT_COMPLETED it in my command. But when I restart my android phone, it not works.

    What's wrong with my service?

    Thanks in advice.
     

    Advertisement

    pothree381 likes this.
  2. pothree381

    pothree381 Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Oct 13, 2017

    Oct 13, 2017
    1
    0
    5

Share This Page

Loading...