BOOT_COMPLETED not working Android

你说的曾经没有我的故事 提交于 2019-11-26 11:16:44
Piyush

This below thing worked for me

AndroidManifest.xml

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

<application>    

    <receiver android:name=".BootCompletedReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <service android:name="NotifyingDailyService" >
    </service>

BootCompletedReceiver.class

public class BootCompletedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    // TODO Auto-generated method stub
    Log.w("boot_broadcast_poc", "starting service...");
    context.startService(new Intent(context, NotifyingDailyService.class));
}

}

Service.class

 public class NotifyingDailyService extends Service {

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
    Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");

    return super.onStartCommand(pIntent, flags, startId);
}
}
Think Twice Code Once

This is an old and basic question but a lot of Android developers now still confused about this trouble, because THEY DON'T TAKE TIME TO READ THE DOCS CAREFULLY

I saw someone shared some links and said that: "This won't work anymore", it's totally wrong and misunderstood.

About this concern: "I've seen this answer said BOOT_COMPLETED is not sent to the application unless the user launches your application first, after Android version 3.1", please read these lines (from official docs: https://developer.android.com/about/versions/android-3.1.html#launchcontrols) to understand correctly:

  • Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.

  • Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications). (They mean force stop an app)

  1. That means a user should launch app at least once after installation to activate the application, then the app can receive implicit broadcasts from OS as normal. (Just only one time launching ever !)

  2. "Does any app that gets installed and never open even only one time ever ?", yep, it 's spam and scam apps, this technique helps user to prevent that!

FURTHERMORE, UNTIL NOW (Android Oreo 8.0), when Android limits registering implicit broadcasts at Manifest (https://developer.android.com/about/versions/oreo/background.html#broadcasts), several broadcasts are still currently exempted from these limitations. And BOOT_COMPLETED is the first one they mention ! (https://developer.android.com/guide/components/broadcast-exceptions.html)

By the way, this is the best solution I found for this question:

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

<receiver android:name=".BootReceiver" android:enabled="true" android:exported="true">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                <!--For HTC devices-->
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
        </receiver>

Finally, please read the document carefully and Think twice code once :3!

some new tablets and android devices have security application by default. some times these apps lock your auto-start mode. an example of this secure app is MyAsus manager. so you can add "allow auto start" to your apps

And for Htc devices add com.htc.intent.action.QUICKBOOT_POWERON

    <receiver android:enabled="true" android:name=".receivers.BootUpReceiver">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
         </intent-filter>
    </receiver>
Gà Và Sói

The problem is with the device. some devices only allow internal apps to receive this action(example: Android 5.1).

you can add this to your intent filter as work around

action android:name="android.intent.action.USER_PRESENT"

This is triggered after the user unlocks the device.

Willem Luijk

The problem I experienced was that BOOT_COMPLETED and QUICKBOOT_POWERON together does not always triggered my intent when I switched the power off from my Android 6.0.1 panel. I have been searching the Internet for quite a long time and found the solution by adding QUICKBOOT_POWEROFF to the manifest file.

See also:

HTC's "fast boot" is not broadcasting BOOT_COMPLETED intent nor wiping intents from alarm manager

For workaround you need to create a NotificationListener service

if the Device is >= Build.VERSION_CODES.JELLY_BEAN_MR2 and having a battery optimization option as in Huawei devices, then you have to ask for NotificationListener permission and create a NotificationListener service as in below code, then you will get the BOOT_COMPLETED in your receiver

Manifest Receiver :

    <receiver
                android:name=".TestRes"
                android:enabled="true"
                android:exported="true">
                <intent-filter android:priority="1">
                    <category android:name="android.intent.category.DEFAULT"/>
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                    <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                    <action android:name="android.intent.action.USER_PRESENT"/>
                    <action android:name="android.intent.action.REBOOT"/>
                </intent-filter>
 </receiver>

1 create a NotificationListener service :

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class DevAppNotificationListener extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
//        super.onNotificationPosted(sbn);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
   //     super.onNotificationRemoved(sbn);
    }


}

2 check if the NotificationListener is granted or not :

static boolean CheckNotificationLisPermission(Context context)
    {

        return NotificationManagerCompat.getEnabledListenerPackages (context).contains(context.getApplicationContext().getPackageName());

    }

3 if not then ask for NotificationListener permission :

 Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            context.startActivityForResult(intent, callBackResultIntent);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!