Android开机自启动程序
背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字
符串常量表示为 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之
即可。记住,Android框架说:Don''t call me, I''ll call you back。我们要做的是做好接收这个消息的准备,而
实现的手段就是实现一个BroadcastReceiver。
1、界面Activity,BootStartDemo.java文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class BootStartDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 无title requestWindowFeature(Window.FEATURE_NO_TITLE); // 全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); new Thread() { public void run() { try { /* 10秒后关闭页面*/ sleep(10000); } catch (Exception e) { e.printStackTrace(); } finally { finish(); // 关闭页面 } } }.start(); }} |
这段代码很简单,当Activity 启动时,会显示TextView,用它显示你想显示的字样,并且这个页面只显示10秒后消失。
2、接收广播消息:BootBroadcastReceiver.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class BootBroadcastReceiver extends BroadcastReceiver { static final String action_boot="android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(action_boot)){ Intent ootStartIntent=new Intent(context,BootStartDemo.class); ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(ootStartIntent); } }} |
该类继续自 BroadcastReceiver,覆载方法 onReceive 中,检测接收到的 Intent 是否符合
BOOT_COMPLETED,如果符合,则启动BootStartDemo这个Activity。
3、配置文件
(1)AndroidManifest.xml :
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?xml version="1.0" encoding="utf-8"?><!-- 这是一个开机自启动程序 --><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ajie.bootstartdemo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".BootStartDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <span style="color: #ff00ff;"><receiver android:name=".BootBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.HOME" /> </intent-filter> </receiver></span> </application><span style="color: #ff00ff;"><strong><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission></strong></span></manifest> |
注意其中颜色标红那一部分,该节点向系统注册了一个 receiver,子节点 intent-filter 表示接收
android.intent.action.BOOT_COMPLETED 消息。并且还要配置android.permission.RECEIVE_BOOT_COMPLETED权限。
(2)Layout文件,main.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/boottext" android:textColor="#5F2DD2" android:background="#FFFFFF" android:textSize="60px" android:gravity="center_horizontal" /></LinearLayout> |
Android开机自启动程序的更多相关文章
- Android 开机自启动应用
Android启动时,会发出一个系统广播 ACTION_BOOT_COMPLETED,它的字符串常量表示为 “android.intent.action.BOOT_COMPLETED” 开机自启动程序 ...
- 系统开启UAC情形下开机自启动程序如何以管理员权限启动
系统开启UAC情形下开机自启动程序如何以管理员权限启动 题记:本文阐述的是在Windows系统开启UAC的情况下,开机自启动程序需要以管理员权限启动, 系统弹出UAC对话框,用户同意的情形下启动程序 ...
- Android::开机自启动C程序【转】
本文转载自:http://blog.csdn.net/Kaiwii/article/details/7681736 之前一篇博文介绍了shell脚本文件的开机启动,地址是http://blog.chi ...
- Android 开机自启动
首先实现开机自启动: 第一步创建一个广播接收者,如MyBootBroadcastReceiver.java package com.example; import android.content.Br ...
- Fedora 16设置开机自启动程序与Ubuntu的区别
Ubuntu设置开机自启动脚本的方法是:修改/etc/init.d/rc.local这个文件,添加需要启动的程序即可,相关函数如下: void SetSysAutoBoot() { ] = {}; ; ...
- android 开机自启动实现
App的开机自启动可以通过注册广播接收器接收开机广播来实现,具体步骤如下: 1.创建 BroadcastReceiver 的派生类,并重写 onReceive() 函数: /** * Created ...
- Android——开机自启动app
android在开机完成后会发送一个android.intent.action.BOOT_COMPLETED的广播,告诉系统内app们已经开机. 我们可以在需要开机自启动的app中定义一个广播接收器, ...
- win10设置开机自启动程序
问题情境:前两天刚刚给自己的win10系统美化了一下,但发现一个问题,每次开机都需要双击启动一个程序,才能达到一个我想要的效果,所以就在思考能不能将这个程序设为开机自启动项呢? 1.首先,找到启动文件 ...
- linux_设置开机自启动程序脚本
设置开机自启动
随机推荐
- JQuery 方法简写
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- saltstack之(十一)扩展组件salt-returners
场景:每次执行salt任务后,将返回结果存入到数据库,可以做任务跟踪以及历史查看. 1.在node1上安装mysql数据库并启动设置root密码.[root@node1 ~]# yum -y inst ...
- iPhone批量删除照片/视频最好用的方法
iPhone批量删除照片/视频最好用的方法 经过大量的搜索和不懈的尝试,今天终于找到了批量删除iPhone照片最好用的方法, 于是决定写一篇博客为更多的果粉们造福! 1. 通过USB将iPhone手机 ...
- int[] List<int> 排序
; List<,,,,,,}; ,,,,}; List<int> result = allingInts.ToList(); result.Sort(); allingInts = ...
- Vue.2.0.5-生产环境部署
删除警告 为了减少文件大小,Vue 精简独立版本已经删除了所有警告,但是当你使用 Webpack 或 Browserify 等工具时,你需要一些额外的配置实现这点. Webpack 使用 Webpac ...
- MetInfo标签函数及参数
参数标签直接在页面中调用标签代码即可: 函数标签需要在页面PHP嵌入代码中通过参数定义转换方可使用,如$metlang=methtml_lang('-'),点击函数标签代码可查看函数标签详细使用方法: ...
- cocoapods遇到error: RPC failed; curl 56 SSLRead() return error -36问题
在安装cocoapods遇到的问题 [!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master Cloning into ...
- windows安装pip 和easy_install
先安装windows版的easy_install 下载 然后下载pip ,python setup.py install 安装好的 pip和easy_install通常在 python目录的 Scr ...
- Browserify
NodeJS 把 JavaScript 的使用从浏览器端扩展到了服务器端,使得前端开发人员可以用熟悉的语言编写服务器端代码.这一变化使得 NodeJS 很快就流行起来.在 NodeJS 社区中有非常多 ...
- tableview隐藏多余分割线
- (void)setExtraCellLineHidden: (UITableView *)tableView{ UIView *view =[ [UIView alloc]init]; view. ...