我有一个应用程序,在引导后启动Intent,适用于 Android 6 到 Android 9 API 级别28。
但是这个代码在 Android 10 API 级别29上不起作用,广播根本没有接收到任何事件,并且在引导后没有运行 MyClassBroadcastReceiver 的 onReceive 方法。在 Android 10 上需要额外的权限或配置吗?
示例中的 Manifest 部分:
但是这个代码在 Android 10 API 级别29上不起作用,广播根本没有接收到任何事件,并且在引导后没有运行 MyClassBroadcastReceiver 的 onReceive 方法。在 Android 10 上需要额外的权限或配置吗?
示例中的 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上无法运行的部分。
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);
}
}
}
Toast
和Log.i()
,但似乎context.startActivity()
没有启动。 - Carlos.V