`
ai_longyu
  • 浏览: 479292 次
社区版块
存档分类
最新评论

Android实现开机自动运行程序

 
阅读更多

有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service。怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call you back!”总结Android框架,真是说到点子上了。理解这句话的含义,许多有关Android平台上实现某种功能的问题,都能迎刃而解。

使用场景:手机开机后,自动运行程序,在屏幕上显示"Hello. I started!"字样。

背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之即可。记住,Android框架说:Don't call me, I'll call you back。我们要做的是做好接收这个消息的准备,而实现的手段就是实现一个BroadcastReceiver。

代码解析:

1、界面Activity:SayHello.java

  1. packagecom.ghstudio.BootStartDemo;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.TextView;
  5. publicclassSayHelloextendsActivity{
  6. @Override
  7. publicvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. TextViewtv=newTextView(this);
  10. tv.setText("Hello.Istarted!");
  11. setContentView(tv);
  12. }
  13. }

这段代码很简单,当Activity启动时,创建一个TextView,用它显示"Hello. I started!"字样。

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

  1. packagecom.ghstudio.BootStartDemo;
  2. importandroid.content.BroadcastReceiver;
  3. importandroid.content.Context;
  4. importandroid.content.Intent;
  5. publicclassBootBroadcastReceiverextendsBroadcastReceiver{
  6. staticfinalStringACTION="android.intent.action.BOOT_COMPLETED";
  7. @Override
  8. publicvoidonReceive(Contextcontext,Intentintent){
  9. if(intent.getAction().equals(ACTION)){
  10. IntentsayHelloIntent=newIntent(context,SayHello.class);
  11. sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  12. context.startActivity(sayHelloIntent);
  13. }
  14. }
  15. }

该类派生自BroadcastReceiver,覆载方法onReceive中,检测接收到的Intent是否符合BOOT_COMPLETED,如果符合,则启动SayHello那个Activity。

3、配置文件:AndroidManifest.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.ghstudio.BootStartDemo"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  7. <activityandroid:name=".SayHello"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <actionandroid:name="android.intent.action.MAIN"/>
  11. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  12. </intent-filter>
  13. </activity>
  14. <receiverandroid:name=".BootBroadcastReceiver">
  15. <intent-filter>
  16. <actionandroid:name="android.intent.action.BOOT_COMPLETED"/>
  17. </intent-filter>
  18. </receiver>
  19. </application>
  20. <uses-sdkandroid:minSdkVersion="3"/>
  21. <uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
  22. </manifest>

注意其中粗体字那一部分,该节点向系统注册了一个receiver,子节点intent-filter表示接收android.intent.action.BOOT_COMPLETED消息。不要忘记配置android.permission.RECEIVE_BOOT_COMPLETED权限。

完成后,编译出apk包,安装到模拟器或手机中。关机,重新开机。

运行截图:

延伸思考:在多数情况下,要自动运行的不是有界面的程序,而是在后台运行的service。此时,就要用startService来启动相应的service了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics