Android开机自启动程序详解

 更新时间:2013年06月19日 10:44:20   作者:  
本篇文章是对Android开机自启动程序进行了详细的分析介绍,需要的朋友参考下
背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字
符串常量表示为 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之
即可。记住,Android框架说:Don''t call me, I''ll call you back。我们要做的是做好接收这个消息的准备,而
实现的手段就是实现一个BroadcastReceiver。
1、界面Activity,BootStartDemo.java文件
复制代码 代码如下:

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
复制代码 代码如下:

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 :
复制代码 代码如下:

<?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
复制代码 代码如下:

<?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显示出来的页面。

相关文章

  • Android布局控件DrawerLayout实现完美侧滑效果

    Android布局控件DrawerLayout实现完美侧滑效果

    这篇文章主要为大家详细介绍了Android布局控件DrawerLayout实现完美侧滑效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Android 实现九宫格抽奖功能

    Android 实现九宫格抽奖功能

    这篇文章主要介绍了Android 实现九宫格抽奖功能,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-03-03
  • Android App隐私合规检测辅助工具Camille详解

    Android App隐私合规检测辅助工具Camille详解

    Camille是一款Android App隐私合规检测辅助工具,现如今APP隐私合规十分重要,各监管部门不断开展APP专项治理工作及核查通报,不合规的APP通知整改或直接下架,下面小编给大家介绍下Android App隐私合规检测辅助工具Camille,感兴趣的朋友一起看看吧
    2022-02-02
  • Android贝塞尔曲线实现消息拖拽消失

    Android贝塞尔曲线实现消息拖拽消失

    这篇文章主要为大家详细介绍了Android贝塞尔曲线实现消息拖拽消失,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android App中使用AudioManager类来编写音频播放器

    Android App中使用AudioManager类来编写音频播放器

    这篇文章主要介绍了Android App中使用AudioManager类来编写音乐播放器的方法,文中举了一个简单的例子实现了基础的播放暂停和静音等功能,需要的朋友可以参考下
    2016-04-04
  • Kotlin协程Context应用使用示例详解

    Kotlin协程Context应用使用示例详解

    这篇文章主要为大家介绍了Kotlin协程Context应用使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Android简单封装一个MVP基类流程详解

    Android简单封装一个MVP基类流程详解

    MVP是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负责显示。下面这篇文章主要给大家介绍了关于Android从实现到封装MVP的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧
    2023-03-03
  • 关于Android Device Monitor 无法打开问题

    关于Android Device Monitor 无法打开问题

    大家好,本篇文章主要讲的是关于Android Device Monitor 无法打开问题,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • Android之EditText控制禁止输入空格和回车

    Android之EditText控制禁止输入空格和回车

    本文主要介绍了Android中使用EditText控制禁止输入空格和回车的实现代码。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-04-04
  • Android框架组件Lifecycle的使用详解

    Android框架组件Lifecycle的使用详解

    这篇文章主要介绍了Android框架组件Lifecycle的使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07

最新评论