简体   繁体   English

BroadcastReceiver 启动完成后不工作

[英]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.我正在开发一个应用程序在此,我必须通过我已经完成的应用程序在 30-30 秒内发送 android 设备的当前状态。 But the problem I am facing is the app is not starting after boot complete this problem is related to some of android devices.但我面临的问题是应用程序在启动完成后没有启动这个问题与一些 android 设备有关。

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 设备上,此代码正在运行,并且我的应用程序在启动完成后启动,但在某些 android 设备上,它无法正常工作。

Android does not auto-start any application until you manually launch it at least once. Android 在您手动启动至少一次之前不会自动启动任何应用程序。 After that, the applications will automatically start on each Android boot.之后,应用程序将在每次 Android 启动时自动启动。

Add android.permission.RECEIVE_BOOT_COMPLETED permission in your manifest.在清单中添加android.permission.RECEIVE_BOOT_COMPLETED权限。

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.当 Android 完成引导并准备好开始 home 活动时,将发送 home 事件,并且合格的应用程序将自己标识为可引导的候选者。 The system sends out the android.intent.category.HOME and android.intent.category.DEFAULT intents when it's done initializing.系统完成初始化后会发出android.intent.category.HOMEandroid.intent.category.DEFAULT意图。

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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