gpt4 book ai didi

java - 启动完成后 BroadcastReceiver 不工作

转载 作者:行者123 更新时间:2023-12-05 00:00:44 24 4
gpt4 key购买 nike

我正在开发一个应用程序 在这方面,我必须通过我已经完成的应用程序在 30-30 秒内发送 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();
}
}
}

list 文件:

<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 启动时自动启动。

在 list 中添加 android.permission.RECEIVE_BOOT_COMPLETED 权限。

当 Android 完成启动并准备启动 home Activity 时,将发送 home 事件并且符合条件的应用程序将自己标识为可启动候选者。系统在完成初始化后发出 android.intent.category.HOMEandroid.intent.category.DEFAULT Intent 。

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

关于java - 启动完成后 BroadcastReceiver 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70528870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com