Android实现开机自动运行程序
来源:互联网 发布:在线录音机软件 编辑:程序博客网 时间:2024/05/18 00:16
背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之即可。记住,Android框架说:Don't call me, I'll call you back。我们要做的是做好接收这个消息的准备,而实现的手段就是实现一个BroadcastReceiver。
代码解析:
1、界面Activity:SayHello.java
package com.javatiger;import android.app.Activity;import android.os.Bundle;public class SayHello extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }}
这段代码很简单,当Activity启动时,创建一个TextView,用它显示"Hello. I started!"字样。
2、布局文件main.xml
<?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="wrap_content" android:text="@string/hello"/></LinearLayout>
3、接收广播消息:BootBroadcastReceiver.java
package com.javatiger;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class BootBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubif (intent.getAction().equals(ACTION)) {Intent sayHelloIntent = new Intent(context, SayHello.class);sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(sayHelloIntent);} }private static final String ACTION = "android.intent.action.BOOT_COMPLETED";}
该类派生自BroadcastReceiver,覆载方法onReceive中,检测接收到的Intent是否符合BOOT_COMPLETED,如果符合,则启动SayHello那个Activity。
3、配置文件:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.javatiger" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SayHello" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <!-- 捕捉开机广播 --> <receiver android:name=".BootBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/></manifest>
注意其中粗体字那一部分,该节点向系统注册了一个receiver,子节点intent-filter表示接收 android.intent.action.BOOT_COMPLETED消息。不要忘记配置 android.permission.RECEIVE_BOOT_COMPLETED权限。
延伸思考:在多数情况下,要自动运行的不是有界面的程序,而是在后台运行的service。此时,就要用startService来启动相应的 service了。
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android开机自动运行程序
- Android开机自动运行程序
- Android开机自动运行程序
- jQuery学习笔记二
- HP打印机为什么取消不了打印任务
- 如何配置和使用log4net日志(VS2008+SQL2008环境下)
- hibernate中generator属性的意义
- 如何让QToolBar中的button显示文字的解决办法
- Android实现开机自动运行程序
- java编译与类路径设置(一)
- Qt中工具栏是如何换行的
- AS3中Event的target和currentTarget的区别(事件处理的三个阶段)
- hibernate 中HQL语句查询学习
- C# GetPixel and SetPixel
- usb_control_msg函数用法
- Spring2.x中的声明性事务(使用Annotation)
- Spring 事务配置