手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >android开机自启动原理与实现案例(附源码)
android开机自启动原理与实现案例(附源码)
摘要:原理:Android系统通过应用程序自行在系统中登记注册事件(即Intent)来响应系统产生的各类消息。Android系统为应用程序管理功能...

原理:

Android系统通过应用程序自行在系统中登记注册事件(即Intent)来响应系统产生的各类消息。 Android系统为应用程序管理功能提供了大量的API,通过配置Intent和permission来实现各种功能。

开机自启动是通过

<intent-filter>

<action android:name="android.intent.action.BOOT_COMPLETED"/>

<category android:name="android.intent.category.HOME" />

</intent-filter>

和权限<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>实现。

举例:

1.新建一个广播接收器的类:[Java]

复制代码 代码如下:

/*

* $filename: BootBroadcastReceiver.java,v $

* $Date: 2013-6-7 $

* Copyright (C) ZhengHaibo, Inc. All rights reserved.

* This software is Made by Zhenghaibo.

*/

package njupt.zhb.startyouself;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

/*

*@author: ZhengHaibo

*web: http://blog.csdn.net/nuptboyzhb

*mail: zhb931706659@126.com

*2013-6-7 Nanjing,njupt,China

*/

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 StartIntent=new Intent(context,MainActivity.class); //接收到广播后,跳转到MainActivity

StartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(StartIntent);

}

}

}

2.在Manifest.xml文件中注册广播接收机,并且配置权限

注册广播接收机:

复制代码 代码如下:

<>

<receiver android:name=".BootBroadcastReceiver" >

<intent-filter>

<action android:name="android.intent.action.BOOT_COMPLETED"/>

<category android:name="android.intent.category.HOME" />

</intent-filter>

</receiver>

添加权限:

复制代码 代码如下:

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

完成上述步骤后,启动一次程序,完成注册。等下次手机开机时,该软件即会自动启动。

扩展:不仅可以自动启动Activity,也可以启动一个后台服务(Service),只需要修改接收机中onReceive函数中的内容即可!

项目的源代码下载

【android开机自启动原理与实现案例(附源码)】相关文章:

Android里实现退出主程序的提示代码

Android裁剪图片为圆形图片的实现原理与代码

android 设置圆角图片实现代码

Android 软件自动更新功能实现的方法

Android 使用Gallery实现3D相册(附效果图+Demo源码)

Android自定义Style实现方法

Android Intent启动别的应用实现方法

Android调用相机并将照片存储到sd卡上实现方法

Android通过手势实现的缩放处理实例代码

android屏幕全屏的实现代码

精品推荐
分类导航