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

相关内容

热门资讯

透视解谜!wepoker钻石怎... 透视解谜!wepoker钻石怎么看底牌,wepoker有没有机器人-果然一直总是有辅助插件(哔哩哔哩...
实测发现!werplan外挂,... 实测发现!werplan外挂,德普之星辅助器app,解谜教程(有人有挂)-哔哩哔哩1、下载好德普之星...
更值得关注的是!wepoker... 更值得关注的是!wepoker有辅助器吗(透视)辅助工具(有挂细节)-哔哩哔哩1、每一步都需要思考,...
昨日!桂麻圈辅助器!真是存在有... 昨日!桂麻圈辅助器!真是存在有辅助神器(有挂教程)-哔哩哔哩1、让任何用户在无需桂麻圈辅助器安装教程...
玩家必备攻略!wpk俱乐部是做... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
透视辅助!wepoker怎么下... 透视辅助!wepoker怎么下载游戏,wepoker怎么看底牌-好像真的是有辅助神器(哔哩哔哩)1、...
透视智能ai!破解辅助插件we... 透视智能ai!破解辅助插件wepoker,wepoker怎么开辅助,解迷教程(有挂分析)-哔哩哔哩1...
透视中牌率!wepoker轻量... 您好,wepoker轻量版辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
事发当天!广西友乐解码器!其实... 事发当天!广西友乐解码器!其实真的有辅助工具(有挂教程)-哔哩哔哩运广西友乐解码器辅助工具,进入游戏...
目前!wepoker脚本下载(... 目前!wepoker脚本下载(透视)wepoker透视脚本免费下载pc-其实有辅助app(哔哩哔哩)...