V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
9527H
V2EX  ›  Android

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

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

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

      @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   ·   我们的愿景   ·   实用小工具   ·   997 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 22:43 · PVG 06:43 · LAX 15:43 · JFK 18:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.