zoukankan      html  css  js  c++  java
  • android 开机自启动实现

      

      App的开机自启动可以通过注册广播接收器接收开机广播来实现,具体步骤如下:

    1.创建 BroadcastReceiver 的派生类,并重写 onReceive() 函数:

     1 /**
     2  * Created by Haoye on 2016/3/8.
     3  * Copyright © 2016 Haoye All Rights Reserved
     4  */
     5 public class BootReceiver extends BroadcastReceiver {
     6 
     7     @Override
     8     public void onReceive(Context context, Intent intent) {
     9         Intent startIntent = new Intent(context, MainActivity.class);
    10         startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    11         startIntent.setAction(Intent.ACTION_MAIN);
    12         startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    13 
    14         context.startActivity(startIntent);
    15     }
    16 }

    2. 在AndroidManifest.xml 文件中注册广播接收器:

    1 <receiver android:name=".BootReceiver" android:enabled="true">
    2      <intent-filter>
    3           <action android:name="android.intent.action.BOOT_COMPLETED"/>
    4 
    5           <category android:name="android.intent.category.HOME"/>
    6      </intent-filter>
    7 </receiver>

        当然也能在java代码中用 registerReceiver() 函数注册和添加权限,并在需要取消时用 unregisterReceiver() 函数取消;

    3.在AndroidManifest.xml 文件中添加自启动权限:

     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    4.测试:

        安装运行-->关闭手机-->启动手机

        注意自启动权限有没有被禁止...

  • 相关阅读:
    关于OI的文学作品
    HBOI 2020 游记
    从0开始的字符串生活(选手命要没了)
    近两年HBOI选做
    NOI online #3
    2020年“美团杯”程序设计挑战赛题解(目前只有测试赛)
    退群咕咕墙
    JS 获得当前地址栏url
    你了解getBoundingClientRect()?
    字符串与数字相加
  • 原文地址:https://www.cnblogs.com/laishenghao/p/5254525.html
Copyright © 2011-2022 走看看