如何实现安卓开机自启动? - V2EX
9527H

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

  •  
  •   9527H Sep 20, 2022 13331 views
    This topic created in 1340 days ago, the information mentioned may be changed or developed.

    机型 :vivo Y32 前置条件:已经在设置里面, 打开自启动管理权限

    实现代码 AndroidManifest.xml 文件

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" package="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";

      @Ovrride 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 replies    2022-09-20 19:07:33 +08:00
    Kasumi20
        1
    Kasumi20  
       Sep 20, 2022
    这个帖子说明重启手机有用
    9527H
        2
    9527H  
    OP
       Sep 20, 2022
    重启手机有用?
    CharmingCheung
        3
    CharmingCheung  
       Sep 20, 2022
    看看 MIUI 的自启动管理是不是限制了
    CharmingCheung
        4
    CharmingCheung  
       Sep 20, 2022
    跳转指定的应用,是跳其他 App 吗?
    是的话,compileSDK>30 吗?
    是的话,有没有加 query package 的权限?
    CharmingCheung
        5
    CharmingCheung  
       Sep 20, 2022
    @CharmingCheung compileSDK >=30
    9527H
        6
    9527H  
    OP
       Sep 20, 2022
    跳自身应用
    bjzhou1990
        7
    bjzhou1990  
       Sep 20, 2022
    需要开启后台弹出权限,默认不允许在后台弹出界面
    9527H
        8
    9527H  
    OP
       Sep 20, 2022
    给了后台弹出权限也不行
    About     Help     Advertise     Blog     API     FAQ     Solana     3043 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 42ms UTC 02:32 PVG 10:32 LAX 19:32 JFK 22:32
    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