Android 开发 — 开机自启动
Android 开发 — 开机自启动
Android 的开机自启动是通过捕捉开机结束的广播来实现的,手机的启动完后会给出一个BroadcastReceiver,在自己的程序中捕捉即可。
程序下载地址在帮客之家的1号FTP服务器里,下载地址:
FTP地址:ftp://www.bkjia.com
用户名:www.bkjia.com
密码:www.muu.cc
在 2011年LinuxIDC.com\7月\Android 开发 — 开机自启动
下载方法见这里 http://www.bkjia.net/thread-1187-1-1.html
AutoBootReceiver.java 文件:
Java代码- package com.ldq.auto.boot;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class AutoBootReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
- Log.i("------", "AutoBootReceiver auto boot");
- Intent in = new Intent(context, ExAutoBoot.class);
- in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//添加Flags必不可少,否则报错www.bkjia.com
- context.startActivity(in);
- }
- }
- }
ExAutoBoot.java 文件:
Java代码- package com.ldq.auto.boot;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- public class ExAutoBoot extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Log.i("------","ExAutoBoot auto boot");
- }
- }
AndroidManifest.xml 文件:
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.ldq.auto.boot" android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".ExAutoBoot" 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="AutoBootReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED"></action>
- </intent-filter>
- </receiver>
- </application>
- <uses-sdk android:minSdkVersion="4" />
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
- </manifest>
教你开启自启动程序! 在电脑的操作系统中都能在电脑开机后自启动一些程序,在Android平台也是可以的,那么我们如何才能做到这样的效果呢。
1、需要BroadcastReceiver
2、使用interfilter中的action:android.intent.action.BOOT_COMPLETED //当启动手机系统启动完成后就启动此Receiver
功能:当手机系统启动完成后,直接启动某个程序或者Activity,这里直接启动某个程序
一下是代码分析:
AndroidManifest.xml文件内容:
- # <?xml version="1.0" encoding="utf-8"?>
- # <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- # package="cc.androidos.sms"
- # android:versionCode="1"
- # android:versionName="1.0.0">
- # <application android:icon="@drawable/icon" android:label="@string/app_name">
- # <activity android:name=".StartUp"
- # 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=".BootReceiver">
- # <intent-filter>
- # <action android:name="android.intent.action.BOOT_COMPLETED" />
- # </intent-filter>
- # </receiver>
- # <service android:name=".StartService"/>
- # </application>
- # </manifest>
要启动的Activity类: Java代码
- # package cc.androidos.sms;
- # import android.app.Activity;
- # import android.os.Bundle;
- # public class StartUp extends Activity {
- # /** Called when the activity is first created. */
- # @Override
- # public void onCreate(Bundle savedInstanceState) {
- # super.onCreate(savedInstanceState);
- # setContentView(R.layout.main);
- # }
- # }
Receiver类:系统启动后接受信息的类
Java代码- # package cc.androidos.sms;
- # import android.app.Activity;
- # import android.app.PendingIntent;
- # import android.content.BroadcastReceiver;
- # import android.content.Context;
- # import android.content.Intent;
- # import android.net.Uri;
- # import android.util.Log;
- # public class BootReceiver extends BroadcastReceiver
- # {
- # @Override
- # public void onReceive( Context context, Intent intent )
- # {
- # if(intent.getAction().equals( Intent.ACTION_BOOT_COMPLETED )){
- # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
- # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
- # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
- # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
- # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
- # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
- # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
- # Intent i = new Intent(context,StartUp.class);
- # i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
- # //使用Receiver直接启动Activity时候需要加入此flag,www.bkjia.com否则系统会出现异常
- # context.startActivity( i );
- #
- # }
- # }
- # }
第一个运行完成后,关闭手机模拟器或者手机,然后启动手机操作系统,启动完成后StartUp Activity会自动运行。
流程: 系统启动完成-------》通过AndroidManifest.xml了解到系统启动完成后要启动BootReceiver -------》BootReceiver 启动StartUp Activity。
评论暂时关闭