博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android开机自启动程序
阅读量:7094 次
发布时间:2019-06-28

本文共 3088 字,大约阅读时间需要 10 分钟。

背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字

符串常量表示为 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之
即可。记住,Android框架说:Don''t call me, I''ll call you back。我们要做的是做好接收这个消息的准备,而
实现的手段就是实现一个BroadcastReceiver。

1、界面Activity,BootStartDemo.java文件

public
class
BootStartDemo
extends
Activity {
    
    
@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
{
                    
                    
sleep(
10000
);
                
}
catch
(Exception e) {
                    
e.printStackTrace();
                
}
finally
{
                    
finish();
// 关闭页面
                
}
            
}
        
}.start();
 
    
}
}

这段代码很简单,当Activity 启动时,会显示TextView,用它显示你想显示的字样,并且这个页面只显示10秒后消失。

2、接收广播消息:BootBroadcastReceiver.java

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 :

<?xml version=
"1.0"
encoding=
"utf-8"
?>
<!-- 这是一个开机自启动程序 -->
<manifest xmlns: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

<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns: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>
完成后,编译出apk包,安装到模拟器或手机中。关机,重新开机,就会显示BootStartDemo这个Activity显示出来的页面。

转载于:https://www.cnblogs.com/xiaowangba/archive/2012/12/11/6314386.html

你可能感兴趣的文章
vim 代码提示功能,让vim可以媲美IDE(转)
查看>>
Hive Explain(翻译自Hive wiki)
查看>>
node开子线程模块--tagg2
查看>>
c# excel xls保存
查看>>
php curl的隐藏BUG
查看>>
程序员眼中的中国传统文化-王阳明《传习录》
查看>>
1216 递归下降分析法
查看>>
ajax四,封装ajax并优化
查看>>
剑指offer 第九天
查看>>
爬取维基百科人物介绍,并使用pymysql存储到数据库
查看>>
[工具]查看一个网站是用什么语言编写的
查看>>
day4-RHCS
查看>>
.NET代码设计简单规范
查看>>
Microsoft.AspNet.SignalR实现弹幕(即时通讯)
查看>>
菜鸡互啄队 --- 第三周-需求改进&系统设计
查看>>
面向对象的多态
查看>>
配置Apache虚拟目录
查看>>
02.驱动调试
查看>>
C# 动态调用WebService
查看>>
在DataList控件访问子控件的方法
查看>>