Android开机自启动具体操作方法简介

移动开发 Android
Android开机自启动可以通过定义一个BroadcastReceiver;配置Receiver的许可等方式来实现。在这里就对这些步骤进行一个详细介绍。

在模拟器中对Android 操作系统进行相应的编写,可以帮助我们实现应用程序的开机自启动功能。在这里我们就来通过一段代码充分的掌握Android开机自启动的相关实现方法,以帮助大家掌握这一应用。

1.定义一个BroadcastReceiver

Java代码

  1. public class BootReceiver extends BroadcastReceiver {   
  2. public void onReceive(Context ctx, Intent intent) {   
  3. Log.d("BootReceiver", "system boot completed");   
  4. //start activity   
  5. String action="android.intent.action.MAIN";   
  6. String category="android.intent.category.LAUNCHER";   
  7. Intent myi=new Intent(ctx,CustomDialog.class);   
  8. myi.setAction(action);   
  9. myi.addCategory(category);   
  10. myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
  11. ctx.startActivity(myi);   
  12. //start service   
  13. Intent s=new Intent(ctx,MyService.class);   
  14. ctx.startService(s);   
  15. }   
  16. }   
  17. public class BootReceiver extends BroadcastReceiver {  
  18. public void onReceive(Context ctx, Intent intent) {  
  19. Log.d("BootReceiver", "system boot completed");  
  20. //start activity  
  21. String action="android.intent.action.MAIN";  
  22. String category="android.intent.category.LAUNCHER";  
  23. Intent myi=new Intent(ctx,CustomDialog.class);  
  24. myi.setAction(action);  
  25. myi.addCategory(category);  
  26. myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  27. ctx.startActivity(myi);  
  28. //start service  
  29. Intent s=new Intent(ctx,MyService.class);  
  30. ctx.startService(s);  
  31. }  

2.配置Receiver的许可,允许接收系统启动消息,在AndroidManifest.xml中:

Xml代码

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

3.配置Receiver,可以接收系统启动消息,在AndroidManifest.xml中

Android开机自启动的Xml代码

  1. < receiver android:name=".app.BootReceiver">   
  2. < intent-filter>   
  3. < action android:name="android.intent.action.BOOT_COMPLETED"/>   
  4. < category android:name="android.intent.category.HOME" />   
  5. < /intent-filter>   
  6. < /receiver>   
  7. < receiver android:name=".app.BootReceiver"> 
  8. < intent-filter> 
  9. < action android:name="android.intent.action.BOOT_COMPLETED"/> 
  10. < category android:name="android.intent.category.HOME" /> 
  11. < /intent-filter> 
  12. < /receiver> 

4.启动模拟器,可以看到系统启动后,弹出一个对话框。

Android开机自启动的具体方法就为大家介绍到这里。

【编辑推荐】

  1. Android显示网络图片相关实现方法浅谈 
  2. Android应用技巧总结 
  3. Android Theme详细内容概述 
  4. Android重力感应实现方式简介 
  5. Android HelloWord编写方式介绍 
责任编辑:曹凯 来源: javaeye.com
相关推荐

2009-12-30 16:48:52

Silverlight

2010-01-28 16:14:33

Android安装卸载

2010-02-03 13:26:53

C++计时

2010-04-02 08:42:32

Oracle 游标

2010-02-02 17:13:35

C++ Endian

2010-02-02 13:57:31

C++解析#pragm

2010-01-07 15:37:35

VB.NET ForNext循环

2010-03-05 15:27:06

Python文件路径

2010-04-13 17:00:27

Oracle NLS_

2010-02-02 16:23:46

C++实现WPF动画

2010-08-02 17:20:12

nfs启动Linux

2010-07-26 13:30:23

2010-03-19 14:19:58

Python正则表达式

2010-03-05 17:06:26

Python显示UTF

2009-12-30 15:47:40

Silverlight

2011-03-17 09:06:58

iptables映射端口

2010-01-27 14:01:19

Android命令行启

2009-12-31 15:36:13

SilverLight

2009-12-31 15:36:13

SilverLight

2010-08-02 12:34:09

ADSL代理设置
点赞
收藏

51CTO技术栈公众号