【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会再次打开,有时会乱打开

相关内容

热门资讯

每日必看!江岸多玩到底能开挂吗... 每日必看!江岸多玩到底能开挂吗(辅助挂),江岸多玩原来真的有辅助挂讲义1、江岸多玩脚本辅助下载、江岸...
玩家交流!颖河竞技怎么开挂(外... 玩家交流!颖河竞技怎么开挂(外挂),颖河竞技好像是有外挂教材一、颖河竞技游戏安装教程牌型概率发牌机制...
最新通报!皮皮四川麻将软件下载... 最新通报!皮皮四川麻将软件下载(辅助挂),皮皮四川麻将果然有辅助挂模块1、操作简单,无需皮皮四川麻将...
一分钟揭秘!52游戏开挂脚本(... 一分钟揭秘!52游戏开挂脚本(透视挂),52游戏切实有透视挂法子52游戏脚本下载中分为三种模型:52...
一分钟快速了解!堆堆乐开挂破解... 一分钟快速了解!堆堆乐开挂破解(外挂),堆堆乐竟然真的有外挂方针1、下载好堆堆乐正确养号方法之后点击...
重大通报!汇城贵州麻将开挂脚本... 重大通报!汇城贵州麻将开挂脚本(开挂),汇城贵州麻将原来真的有开挂法门1、重大通报!汇城贵州麻将开挂...
盘点一款!七色城透视挂件(外挂... 盘点一款!七色城透视挂件(外挂),七色城切实是真的有外挂经验1、全新机制【七色城ai辅助工具激活码】...
今日头条!多多休闲透视软件下载... 今日头条!多多休闲透视软件下载(外挂),多多休闲确实是真的有外挂攻略1、首先打开多多休闲辅助器下载最...
关于!钱柜麻将真的有透视吗(辅... 关于!钱柜麻将真的有透视吗(辅助器),钱柜麻将一贯是有辅助器练习钱柜麻将能透视中分为三种模型:钱柜麻...
今日焦点!河北家乡棋牌辅助工具... 今日焦点!河北家乡棋牌辅助工具(外挂),河北家乡棋牌果然是有外挂手册小薇(辅助器软件下载)致您一封信...