Android设备开机时自动运行程序
来源:互联网 发布:网络用语吃辣条啥意思 编辑:程序博客网 时间:2024/05/02 01:26
该实例要实现的功能是,在Android手机开机后,自动运行实例程序,在屏幕上显示文字“Hello. I started!”。
背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之即可。记住,高焕堂先生对Android框架的总结:Don't call me, I'll call you back。我们要做的是做好接收这个消息的准备,而实现的手段就是实现一个BroadcastReceiver。
Android程序开机自动运行的实例代码解析:
1、界面Activity:SayHello.java
- package com.ghstudio.BootStartDemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class SayHello extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TextView tv = new TextView(this);
- tv.setText("Hello. I started!");
- setContentView(tv);
- }
- }
这段代码很简单,当Activity启动时,创建一个TextView,用它显示"Hello. I started!"字样。
2、接收广播消息:BootBroadcastReceiver.java
- package com.ghstudio.BootStartDemo;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- public class BootBroadcastReceiver extends BroadcastReceiver {
- static final String ACTION = "android.intent.action.BOOT_COMPLETED";
- @Override
- public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals(ACTION)){
- Intent sayHelloIntent=new Intent(context,SayHello.class);
- sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(sayHelloIntent);
- }
- }
- }
该类派生自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.ghstudio.BootStartDemo"
- 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>
- <receiver android:name=".BootBroadcastReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- </intent-filter>
- </receiver>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
- </manifest>
注意其中粗体字那一部分,该节点向系统注册了一个receiver,子节点intent-filter表示接收android.intent.action.BOOT_COMPLETED消息。不要忘记配置android.permission.RECEIVE_BOOT_COMPLETED权限。
完成后,编译出apk包,安装到模拟器或手机中。关机,重新开机。
运行截图:
延伸思考:在多数情况下,要自动运行的不是有界面的程序,而是在后台运行的service。此时,就要用startService来启动相应的service了。
- Android设备开机时自动运行程序
- Android开机自动运行程序
- Android开机自动运行程序
- Android开机自动运行程序
- android 开机自动运行程序
- Android 开机自动运行程序
- 开机自动运行程序【Android】
- Android开机自动运行程序
- android 开机自动运行程序
- Android程序如何在开机时自动运行
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- android应用开机自动运行程序
- Android实现开机自动运行程序
- Android实现开机自动运行程序
- android应用开机自动运行程序
- android应用开机自动运行程序
- JAVA基础day03 循环 函数运用及函数的重载
- 程序员之路——一个老程序员对刚上大学的学弟学妹的忠告
- caffe CNN train_val.prototxt 神经网络参数配置说明
- MySQL5.7 group by新特性,报错1055
- 进制转换算法
- Android设备开机时自动运行程序
- oracle insert into values 多条记录
- TCP-IP详解:糊涂窗口综合症(Silly Window syndrome)
- HDFS java客户端使用
- 第二周项目2-程序的多文件组织
- 初识Spring
- 三元函数的复习
- Apgache Common
- [Codeforces #275 (Div. 2)B. Friends and Presents] 二分