簡體   English   中英

BroadcastReceiver 啟動完成后不工作

[英]BroadcastReceiver is not working after boot complete

我正在開發一個應用程序在此,我必須通過我已經完成的應用程序在 30-30 秒內發送 android 設備的當前狀態。 但我面臨的問題是應用程序在啟動完成后沒有啟動這個問題與一些 android 設備有關。

我的代碼是:

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();
        }
    }
}

清單文件:

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

項目清單

在某些 android 設備上,此代碼正在運行,並且我的應用程序在啟動完成后啟動,但在某些 android 設備上,它無法正常工作。

Android 在您手動啟動至少一次之前不會自動啟動任何應用程序。 之后,應用程序將在每次 Android 啟動時自動啟動。

在清單中添加android.permission.RECEIVE_BOOT_COMPLETED權限。

當 Android 完成引導並准備好開始 home 活動時,將發送 home 事件,並且合格的應用程序將自己標識為可引導的候選者。 系統完成初始化后會發出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