【Android】Android10及以上实现开机自动启动
创始人
2025-01-10 19:34:52
0

1、使用广播接收器实现

1.1 创建广播接收器 StartReceiver继承BroadcastReceiver

package com.example.testautoopenphone;  import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;  public class StartReceiver extends BroadcastReceiver {     static final String ACTION = "android.intent.action.BOOT_COMPLETED";     @Override     public void onReceive(Context context, Intent intent) {         if (intent.getAction().equals(ACTION)) {             Intent mainActivityIntent = new Intent(context, MainActivity.class);  // 要启动的Activity             mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);             context.startActivity(mainActivityIntent);         }         if(intent.getAction().equals("WALLPAPER_CHANGED")){             System.out.println("=============");         }     }  }

1.2 在AndroidManifest.xml中注册广播,放在application里面activity后面

                                                                                                       

 

1.3  在AndroidManifest.xml声明权限

                   

 

1.4 在手机上打开自启动权限相关设置

1.5 存在问题,修改授权设置后重新授权后,不能开机自动启动了

2、使用JobScheduler实现

2.1 创建任务服务,BootJobService继承JobService

package com.example.testautoopenphone;  import android.app.job.JobInfo; import android.app.job.JobParameters; import android.app.job.JobService; import android.content.Intent; import android.util.Log;  public class BootJobService extends JobService {      private static final String TAG = "BootJobService";      @Override     public boolean onStartJob(JobParameters jobParameters) {         // 在这里执行你的启动逻辑           Log.d(TAG, "onStartJob: Boot completed, starting job.");  //        if (!Settings.canDrawOverlays(this)) { //            //若未授权则请求权限 //            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); //            intent.setData(Uri.parse("package:" + getPackageName())); //            startActivityForResult(intent, 0); //        }          // 例如,启动一个Activity或者执行其他任务           Intent intent = new Intent(this, MainActivity.class);         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         startActivity(intent);          // 返回true表示任务完成,不需要再次调度           return true;     }      @Override     public boolean onStopJob(JobParameters jobParameters) {         // 如果任务被系统停止(例如设备进入省电模式),在这里处理相关逻辑           Log.d(TAG, "onStopJob: Job stopped by system.");         return true;     } }

 

2.2 在AndroidManifest.xml中注册服务,放在application里面activity后面

                                                                             

 

 2.3 在MainActivity.java使用jobScheduler

package com.example.testautoopenphone;  import androidx.appcompat.app.AppCompatActivity; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; import android.os.Build;  import android.os.Bundle;  public class MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {             scheduleJob();         }     }      private void scheduleJob() {         JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);         if (jobScheduler != null) {             int jobId = 1;             ComponentName serviceComponent = new ComponentName(this, BootJobService.class);              JobInfo.Builder builder = new JobInfo.Builder(jobId, serviceComponent);              // 设置Job为开机启动             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {                 builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);                 builder.setRequiresDeviceIdle(false);                 builder.setRequiresStorageNotLow(false);                 builder.setRequiresBatteryNotLow(false);                 builder.setPersisted(true); // 使Job在设备重启后仍然存在             } else {                 // 对于Android 7.1及以下版本,需要使用其他方法触发Job,因为BOOT_COMPLETED不再可用             }              JobInfo jobInfo = builder.build();              try {                 jobScheduler.schedule(jobInfo);             } catch (Exception e) {                 e.printStackTrace();             }         }     } }

2.4  在手机上打开自启动权限相关设置

2.5 存在问题,有时打开App会再次打开,有时会乱打开

相关内容

热门资讯

曝光一下!决战卡五星辅助,威信... 曝光一下!决战卡五星辅助,威信茶馆app插件,原来有挂(哔哩哔哩)1、威信茶馆app插件免费辅助多个...
分享一下!微信微乐小程序修改器... 分享一下!微信微乐小程序修改器,永胜联盟会封号吗,真是真的有挂(哔哩哔哩)1、许多玩家不知道微信微乐...
辅助一下!微乐小程序辅助脚本,... 辅助一下!微乐小程序辅助脚本,闲逸免费app辅助,切实真的是有挂(哔哩哔哩);该软件可以轻松地帮助玩...
教你一下!芒果辅助器安卓版,海... 教你一下!芒果辅助器安卓版,海盗来了辅助脚本,原来有挂(哔哩哔哩)1、玩家可以在芒果辅助器安卓版线上...
解谜一下!拱趴大菠萝运势,天天... 解谜一下!拱趴大菠萝运势,天天微友有辅助吗,本来真的是有挂(哔哩哔哩)天天微友有辅助吗透视方法中分为...
解谜一下!边锋干瞪眼ios辅助... 解谜一下!边锋干瞪眼ios辅助,皇豪互众控制系统,其实是有挂(哔哩哔哩)1、该软件可以轻松地帮助玩家...
曝光一下!欢乐茶馆挂,大菠萝辅... 曝光一下!欢乐茶馆挂,大菠萝辅助器,好像是有挂(哔哩哔哩)1、大菠萝辅助器公共底牌简单,大菠萝辅助器...
必备一下!四川游戏家园破解版,... 必备一下!四川游戏家园破解版,微乐家乡麻辣自建房辅助app,原来有挂(哔哩哔哩)1、完成微乐家乡麻辣...
总结一下!可乐辅助,威信茶馆解... 总结一下!可乐辅助,威信茶馆解码器,一贯真的是有挂(哔哩哔哩)该软件可以轻松地帮助玩家将威信茶馆解码...
详细一下!透视辅助工具,兴动休... 详细一下!透视辅助工具,兴动休闲辅助,一贯是真的有挂(哔哩哔哩)1、完成透视辅助工具辅助器v3.3的...