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

1、界面Activity,BootStartDemo.java文件

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class BootStartDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // 无title requestWindowFeature(Window.FEATURE_NO_TITLE); // 全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); new Thread() { public void run() { try { /* 8秒后关闭页面*/ sleep( 10000 ); } catch (Exception e) { e.printStackTrace(); } finally { finish(); // 关闭页面 } } }.start(); } }

这段代码很简单,当Activity 启动时,会显示TextView,用它显示你想显示的字样,并且这个页面只显示10秒后消失。

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class BootBroadcastReceiver extends BroadcastReceiver { static final String action_boot= "android.intent.action.BOOT_COMPLETED" ; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(action_boot)){ Intent ootStartIntent= new Intent(context,BootStartDemo. class ); ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(ootStartIntent); } } }

该类继续自 BroadcastReceiver,覆载方法 onReceive 中,检测接收到的 Intent 是否符合
BOOT_COMPLETED,如果符合,则启动BootStartDemo这个Activity。

3、配置文件

(1)AndroidManifest.xml :

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?xml version= "1.0" encoding= "utf-8" ?> <!-- 这是一个开机自启动程序 --> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = "com.ajie.bootstartdemo" android:versionCode= "1" android:versionName= "1.0" > <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ".BootStartDemo" android:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> <span style= "color: #ff00ff;" ><receiver android:name= ".BootBroadcastReceiver" > <intent-filter> <action android:name= "android.intent.action.BOOT_COMPLETED" /> <category android:name= "android.intent.category.HOME" /> </intent-filter> </receiver> </span> </application> <span style= "color: #ff00ff;" ><strong><uses-permission android:name= "android.permission.RECEIVE_BOOT_COMPLETED" ></uses-permission></strong> </span></manifest>

注意其中颜色标红那一部分,该节点向系统注册了一个 receiver,子节点 intent-filter 表示接收
android.intent.action.BOOT_COMPLETED 消息。并且还要配置android.permission.RECEIVE_BOOT_COMPLETED权限。

(2)Layout文件,main.xml

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?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= "fill_parent" android:text= "@string/boottext" android:textColor= "#5F2DD2" android:background= "#FFFFFF" android:textSize= "60px" android:gravity= "center_horizontal" /> </LinearLayout>

完成后,编译出apk包,安装到模拟器或手机中。关机,重新开机,就会显示BootStartDemo这个Activity显示出来的页面。

本文转载地址:http://www.cnblogs.com/fbsk/archive/2011/10/10/2205316.html

更多相关文章

  1. Android ViewPager多页面滑动切换以及单页卡内添加事件
  2. 在android中显示网络图片及查看页面源代码
  3. Android登录界面开发及响应;页面跳转;传参
  4. Android studio 页面布局无法显示问题
  5. android 进入页面隐藏输入法
  6. activity页面切换效果
  7. Android成长(二)——两个页面交互
  8. [Android] Activity页面切换效果
  9. android锁屏页面的实现

随机推荐

  1. Android两种播放视频的方法(SurfaceView
  2. Android 快速选择联系人
  3. Android 获得本机ip地址和MAC地址
  4. 输入法弹出后,不影响activity原有布局(and
  5. android pdf 阅读器开发, pdf demo, pdf
  6. Android ListView设置Item选中状态
  7. Android Q中外部存储盘路径正则表达式的
  8. android 中调用接口发送短信
  9. 转载:Android(安卓)实用工具Hierarchy Vie
  10. Android(安卓)文件存储