简体   繁体   中英

BroadcastReceiver is not working after boot complete

I am working on an app In this, I have to send the current status of the android devices in 30-30 seconds through my app which I have done. But the problem I am facing is the app is not starting after boot complete this problem is related to some of android devices.

MY Code is:

public class BootCompletedReceiver extends BroadcastReceiver {

    private static final String TAG = "BootCompletedReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG,"action : "+intent.getAction());
        if (Objects.requireNonNull(intent.getAction()).equals(Intent.ACTION_BOOT_COMPLETED)){
            Log.d(TAG,"receive boot completes : "+intent.getAction());
            @SuppressLint("StaticFieldLeak")
            AsyncTask task = new AsyncTask() {
                @Override
                protected Object doInBackground(Object[] objects) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @RequiresApi(api = Build.VERSION_CODES.O)
                @Override
                protected void onPostExecute(Object o) {
                    super.onPostExecute(o);
                    context.startForegroundService(new Intent(context, StatUpdateService.class));
                }
            };

            task.execute();
        }
    }
}

Manifests file:

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

        <receiver
            android:name=".receiver.BootCompletedReceiver"
            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" />
            </intent-filter>
        </receiver>

List item

On Some of the android devices, This Code is working and My app is starting after boot complete, but on the Some of the android devices, it's not working.

Android does not auto-start any application until you manually launch it at least once. After that, the applications will automatically start on each Android boot.

Add android.permission.RECEIVE_BOOT_COMPLETED permission in your manifest.

When Android finishes booting and is ready to start the home activity, the home event is sent and qualifying applications identify themselves as bootable candidates. The system sends out the android.intent.category.HOME and android.intent.category.DEFAULT intents when it's done initializing.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.test.app">

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

    <application
        android:theme="@style/app_theme_red">

        <!-- main -->
        <activity
            android:name=".ui.activity.MainActivity"
            android:exported="true">

            <!-- main filter -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

                <!-- add this -->
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <!-- boot receiver -->
        <receiver android:name=".BootReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

    </application>
    
</manifest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM