阅读量:1
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后面
<receiver android:name="com.example.testautoopenphone.StartReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.WALLPAPER_CHANGED"/> </intent-filter> </receiver>
1.3 在AndroidManifest.xml声明权限
<!-- 配置开机启动权限--> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!--悬浮窗--> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
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后面
<service android:name="com.example.testautoopenphone.BootJobService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"> <intent-filter> <action android:name="com.example.yourapp.BOOT_JOB"/> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </service>
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会再次打开,有时会乱打开