[Android实例] Android实现开机自动运行程序

 

From http://www.eoeandroid.com/forum.php?mod=viewthread&tid=30620 

 

背景知识:当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. package com.ghstudio.BootStartDemo;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.TextView;

  5. public class SayHello extends Activity {

  6.     @Override
  7.     public void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);


  9.         TextView tv = new TextView(this);
  10.         tv.setText("Hello. I started!");


  11.         setContentView(tv);
  12.     }
  13. }
复制代码
这段代码很简单,当Activity启动时,创建一个TextView,用它显示"Hello. I started!"字样。

2、接收广播消息:BootBroadcastReceiver.java
  1. package com.ghstudio.BootStartDemo;

  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;

  5. public class BootBroadcastReceiver extends BroadcastReceiver {

  6. static final String ACTION = "android.intent.action.BOOT_COMPLETED";

  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9.   
  10.   if (intent.getAction().equals(ACTION)){
  11.    Intent sayHelloIntent=new Intent(context,SayHello.class);
  12.    sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  13.    context.startActivity(sayHelloIntent);
  14.   }
  15. }

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

3、配置文件:AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.ghstudio.BootStartDemo"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".SayHello"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.   [b]<receiver android:name=".BootBroadcastReceiver">
  15.   <intent-filter>
  16.     <action android:name="android.intent.action.BOOT_COMPLETED" />
  17.    </intent-filter>
  18.   </receiver> [/b]    </application>
  19.     <uses-sdk android:minSdkVersion="3" />

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

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

    延伸思考:在多数情况下,要自动运行的不是有界面的程序,而是在后台运行的service。此时,就要用startService来启动相应的 service了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值