【Android】Android10及以上实现开机自动启动

您所在的位置:网站首页 安卓开机自启指定应用 【Android】Android10及以上实现开机自动启动

【Android】Android10及以上实现开机自动启动

2024-07-09 23:26:31| 来源: 网络整理| 查看: 265

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



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭