boot_completed无法在Android 10 Q API级别29上运行 [英] boot_completed not working on Android 10 Q API level 29
问题描述
我需要帮助.
我有一个应用程序,该应用程序在从Android 6到Android 9 API级别28的启动过程中启动了Intent.但是此代码在Android 10 API级别29上不起作用,广播在启动后根本不会接收任何事件,并且不会在MyClassBroadcastReceiver上的onReceive上运行.需要在android 10系统或配置上进行任何额外的许可吗?
I have an application that starts an Intent after the boot that works from Android 6 to Android 9 API level 28. But this code does not work on Android 10 API level 29, Broadcast simply does not receive any events and does not run onReceive on MyClassBroadcastReceiver after the boot. is there any extra permission on the android 10 system or configuration that needs to be done?
示例的干燥部分:清单:
Dry part of the example: Manifest:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.softniels.autostartonboot">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.softniels.autostartonboot.ForegroundService"
android:label="My Service">
<intent-filter>
<action android:name="com.softniels.autostartonboot.ForegroundService" />
</intent-filter>
</service>
<receiver
android:name=".StartMyServiceAtBootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
这是无法在Android 10上运行的部分.
Here the part that doesn't run on Android 10.
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.i("onReceive", "call onReceive ACTION_BOOT_COMPLETED");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
推荐答案
我知道这可能已经很老了,但是我也遇到了同样的问题,并据此: https://developer.android.com/guide/components/activities/background-starts
I know that this may be old but I have faced the same problem and according to this: https://developer.android.com/guide/components/activities/background-starts
我想出的最简单的解决方案就是添加
The easiest solution I came up with was simply adding
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
并设置接收器:
<receiver
android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
到清单.
接收方代码:
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// Intent n = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
// n.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
// Intent.FLAG_ACTIVITY_CLEAR_TASK);
// context.startActivity(n);
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
两个选项均有效.我看到的唯一缺点是,加载应用程序需要花费相当长的时间(测试后最多可能需要10秒的时间)
Both options work. The only downside I see is that it takes rather a while for app to load (can be up to 10 seconds from my testings)
如果其他人也遇到此问题,请将其留在这里.这仅适用于android 10及更高版本.
Leaving this here for other people if they encounter this as well. This only applies to android 10 and up.
如@ legolas108所述,这需要图形叠加,可以使用以下方法完成:
As stated by @legolas108, this requires drawing overlay, which can be done with:
if (!Settings.canDrawOverlays(getApplicationContext())) {
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
Uri uri = Uri.fromParts("package", getPackageName(), null);
myIntent.setData(uri);
startActivityForResult(myIntent, REQUEST_OVERLAY_PERMISSIONS);
return;
}
这篇关于boot_completed无法在Android 10 Q API级别29上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!