【问题标题】:BroadcastReceiver is not working after boot complete启动完成后广播接收器不工作
【发布时间】:2021-12-30 07:59:28
【问题描述】:

我正在开发一个应用程序在此,我必须通过我已经完成的应用程序在 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 设备上,它不工作。

【问题讨论】:

  • 请检查可能是机器配置问题或支持的硬件和软件在您的 IIP 机器上可用。并检查两台 IIP 机器上的 android 版本。
  • 请在 2022 年停止使用 AsyncTask...

标签: java c# android


【解决方案1】:

在您至少手动启动一次之前,Android 不会自动启动任何应用程序。之后,应用程序将在每次 Android 启动时自动启动。

在您的清单中添加android.permission.RECEIVE_BOOT_COMPLETED 权限。

当 Android 完成启动并准备好启动 home Activity 时,将发送 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>

【讨论】:

  • 感谢您的回答。我在我的项目中添加了 代码,但它不起作用。
  • 从启动图标手动运行您的应用程序(在启动图标上至少单击一次),然后重新启动您的安卓手机。
  • 别忘了,禁用 android.intent.category.HOME 以显示启动图标。
  • 是的,但是我想在启动完成后启动我的服务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-03
  • 2017-05-22
  • 2011-11-25
  • 1970-01-01
相关资源
最近更新 更多