
机型 :vivo Y32 前置条件:已经在设置里面, 打开自启动管理权限
实现代码 AndroidManifest.xml 文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" pckage="com.harry.broadreceiverstart">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><application android:name=".MyApp" 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"> <receiver android:name=".AutoStartBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <activity android:name=".MainActivity" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> AutoStartBroadcastReceiver.java 文件 package com.harry.broadreceiverstart;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.util.Log;
/**
@author Martin-harry
@date 2022/3/2
@Desc 定义自启动广播 */ public class AutoStartBroadcastReceiver extends BroadcastReceiver { private static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override public void onReceive(Context context, Intent intent) { Log.e("接收广播", "自启动 onReceive: " + context.getPackageName()); Log.e("接收广播", "自启动 onReceive: " + intent.getAction()); //开机启动 if (ACTION.equals(intent.getAction())) { //第一种方式 通过包名跳转指定的应用 // PackageManager packageManager = context.getPackageManager(); // Intent mainIntent = packageManager.getLaunchIntentForPackage("com.harry.broadreceiverstart"); // mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // context.startActivity(mainIntent); //context.startService(mainIntent);
//第二种方式 通过指定类跳转指定的应用 Intent mainIntent = new Intent(context, MainActivity.class); mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mainIntent); } } }
现象:可以接受到开机广播,但是跳转指定的应用失败
另一个问题,小米系列的手机,自启动权限打开以后,也不能监听到开机广播
1 Kasumi20 2022-09-20 16:36:17 +08:00 这个帖子说明重启手机有用 |
2 9527H OP 重启手机有用? |
3 CharmingCheung 2022-09-20 16:51:53 +08:00 看看 MIUI 的自启动管理是不是限制了 |
4 CharmingCheung 2022-09-20 16:56:38 +08:00 跳转指定的应用,是跳其他 App 吗? 是的话,compileSDK>30 吗? 是的话,有没有加 query package 的权限? |
@CharmingCheung compileSDK >=30 |
6 9527H OP 跳自身应用 |
7 bjzhou1990 2022-09-20 17:12:14 +08:00 需要开启后台弹出权限,默认不允许在后台弹出界面 |
8 9527H OP 给了后台弹出权限也不行 |