android 开机自启动实现
2016-03-08 16:23
381 查看
App的开机自启动可以通过注册广播接收器接收开机广播来实现,具体步骤如下:
[b]1.创建 BroadcastReceiver 的派生类,并重写 onReceive() 函数:[/b]
/** * Created by Haoye on 2016/3/8. * Copyright © 2016 Haoye All Rights Reserved */ public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent startIntent = new Intent(context, MainActivity.class); startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startIntent.setAction(Intent.ACTION_MAIN); startIntent.addCategory(Intent.CATEGORY_LAUNCHER); context.startActivity(startIntent); } }
[b]2. 在AndroidManifest.xml 文件中注册广播接收器:[/b]
<receiver android:name=".BootReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <category android:name="android.intent.category.HOME"/> </intent-filter> </receiver>
当然也能在java代码中用 registerReceiver() 函数注册和添加权限,并在需要取消时用 unregisterReceiver() 函数取消;
[b]3.在AndroidManifest.xml 文件中添加自启动权限:[/b]
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
[b]4.测试:[/b]
安装运行-->关闭手机-->启动手机
注意自启动权限有没有被禁止...
相关文章推荐
- zhu的工作日记:指定日期往后延多少天(android)
- Android应用开发异常整理
- Android之NDK开发
- Android基础BroadcastReceiver详解
- 使用MVP 书写 Android
- Android 屏幕(View)坐标系统
- Android SharedPreferencesUtils数据保存工具类
- Android simpleadapter对象参数解释
- 从源码剖析Android中的Intent组件
- Android TextInputLayout修改提示信息文字颜色
- Android安全专项-AndBug动态调试工具
- Android 之 Edittext输入四位自动空格
- Android 快速开发系列 打造万能的ListView GridView 适配器
- Android输入的身份证号大小写通用
- Gradle 从入门到精通
- android屏幕自适应&&android 属性
- Gradle 从入门到精通
- Android 中SQLite 增删改查的方法介绍
- The project is using an unsupported version of the Android Gradle plug-in
- Android图片资源获取原则