当前位置:   article > 正文

Android-Android10及以上开机自启APP_android10.0app开机自启动

android10.0app开机自启动

前言:

        帮朋友做一下开机监听,并自启app。顺便记录一下。

注意:开机后会停留在锁屏页面,且短时间内如果没有进行解锁操作,屏幕会进入休眠状态,所以启动APP时需要先唤醒屏幕和解锁屏幕

流程:

一:在AndroidMainfest.xml中,将启动页的Activity设置一下即可(intent-filter)

  1. <activity
  2. android:name=".MainActivity"
  3. android:exported="true">
  4. <intent-filter>
  5. <category android:name="android.intent.category.HOME" />
  6. <action android:name="android.intent.action.MAIN" />
  7. <category android:name="android.intent.category.LAUNCHER" />
  8. <category android:name="android.intent.category.DEFAULT" />
  9. </intent-filter>
  10. </activity>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

二:AndroidManifest文件中声明receiver:

  1. <receiver
  2. android:name=".BroadCastReceiver"
  3. android:enabled="true"
  4. android:exported="true">
  5. <intent-filter android:priority="1000">
  6. <action android:name="android.intent.action.BOOT_COMPLETED" />
  7. </intent-filter>
  8. </receiver>

接下来是权限(一会要用到悬浮窗权限) 

  1. <!--接收启动完成的广播权限-->
  2. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  3. <!--悬浮窗-->
  4. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

然后再在定义的BroadCastReceiver中实现BroadcastReceiver

  1. /**
  2. * 开机自启动
  3. */
  4. public class BroadcastReceiver extends BroadcastReceiver {
  5. static final String ACTION = "android.intent.action.BOOT_COMPLETED";
  6. @Override
  7. public void onReceive(Context context, Intent intent) {
  8. //开机启动
  9. if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
  10. Intent thisIntent = new Intent(context, StartPageActivity.class);//设置要启动的app
  11. thisIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  12. context.startActivity(thisIntent);
  13. }
  14. }
  15. }

三:在主Activity里申请悬浮窗权限(任何一种声明权限的方式都可,我用的RxPermissions )

android6.0不用理会,会自动开

  1. RxPermissions rxPermissions=new RxPermissions(this);
  2. rxPermissions.request(Manifest.permission.SYSTEM_ALERT_WINDOW).subscribe(new Consumer<Boolean>() {
  3. @Override
  4. public void accept(Boolean aBoolean) throws Exception {
  5. if (aBoolean){
  6. //申请的权限全部允许
  7. Toast.makeText(MainActivity.this, "允许了权限!", Toast.LENGTH_SHORT).show();
  8. initData();
  9. }else{
  10. //只要有一个权限被拒绝,就会执行
  11. Toast.makeText(MainActivity.this, "未授权权限,部分功能不能使用", Toast.LENGTH_SHORT).show();
  12. }
  13. }
  14. });

补充:

悬浮必须要申请,不申请无法自启

我的测试机是华为的,需要打开自启动,小米的貌似要打开其他东西,ov好像不用,只需要下图就可以。

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/712808
推荐阅读
相关标签
  

闽ICP备14008679号