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

相关内容

热门资讯

3分钟了解(Wepoke计算器... 3分钟了解(Wepoke计算器)黑科技透明挂辅助软件(透视)新2025教程(2026已更新)(哔哩哔...
wepoke有挂网上靠谱!德普... wepoke有挂网上靠谱!德普之星透视辅助软件是真的(透视)软件教程(有挂细节)-哔哩哔哩;wepo...
8分钟了解!潮汕木虱有辅助挂(... 您好,潮汕木虱有辅助挂这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在...
免费测试版"wepo... 免费测试版"wepoke软件透明挂存在"详细外挂透明挂辅助器-本来是真的有挂(哔哩哔哩);1、不需要...
四分钟了解(WPK开发)软件透... 四分钟了解(WPK开发)软件透明挂辅助ai(透视)2025新版总结(2023已更新)(哔哩哔哩);1...
WePoKe有挂!wpk免费辅... 《WePoKe有挂!wpk免费辅助(透视)wepoke教程(竟然有挂)-哔哩哔哩》 wpk免费辅助软...
十分钟了解!皮皮胡子辅助(辅助... 十分钟了解!皮皮胡子辅助(辅助挂)详细透视开挂辅助细节方法(本来是真的挂)-哔哩哔哩;1、这是跨平台...
一分钟教会你"微扑克... 一分钟教会你"微扑克辅助测试"详细外挂透明挂辅助挂-确实真的是有挂(哔哩哔哩);一分钟教会你"微扑克...
4分钟了解(wepoke挂)黑... 4分钟了解(wepoke挂)黑科技透明挂辅助挂(透视)必赢方法(2021已更新)(哔哩哔哩);1、超...
WePoKe有挂!wepoke... WePoKe有挂!wepoker辅助器安装包(透视)玩家教程(有挂分析)-哔哩哔哩;是一款可以让一直...