如何实现安卓开机自启动? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
9527H
V2EX    Android

如何实现安卓开机自启动?

  •  
  •   9527H 2022-09-20 16:33:29 +08:00 12261 次点击
    这是一个创建于 1147 天前的主题,其中的信息可能已经有所发展或是发生改变。

    机型 :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

    • @address

    • @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); } 

      } }

    现象:可以接受到开机广播,但是跳转指定的应用失败

    另一个问题,小米系列的手机,自启动权限打开以后,也不能监听到开机广播

    8 条回复    2022-09-20 19:07:33 +08:00
    Kasumi20
        1
    Kasumi20  
       2022-09-20 16:36:17 +08:00
    这个帖子说明重启手机有用
    9527H
        2
    9527H  
    OP
       2022-09-20 16:39:27 +08:00
    重启手机有用?
    CharmingCheung
        3
    CharmingCheung  
       2022-09-20 16:51:53 +08:00
    看看 MIUI 的自启动管理是不是限制了
    CharmingCheung
        4
    CharmingCheung  
       2022-09-20 16:56:38 +08:00
    跳转指定的应用,是跳其他 App 吗?
    是的话,compileSDK>30 吗?
    是的话,有没有加 query package 的权限?
    CharmingCheung
        5
    CharmingCheung  
       2022-09-20 16:57:04 +08:00
    @CharmingCheung compileSDK >=30
    9527H
        6
    9527H  
    OP
       2022-09-20 17:09:12 +08:00
    跳自身应用
    bjzhou1990
        7
    bjzhou1990  
       2022-09-20 17:12:14 +08:00
    需要开启后台弹出权限,默认不允许在后台弹出界面
    9527H
        8
    9527H  
    OP
       2022-09-20 19:07:33 +08:00
    给了后台弹出权限也不行
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     989 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 19:19 PVG 03:19 LAX 11:19 JFK 14:19
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86