在使用智能手机的时候,有些程序是一直伴随这我们的,或者说是需要实时反馈和交互的,例如我们手机的主题界面,闹钟程序等等。对于这些程序,我们自然而然的会希望他们能够开机自启动,因为这样子可以避免忘记手动开启某些程序,例如日常闹钟等等,并且省了很多繁琐的事情。
正如高焕堂先生总结Android框架时所说的“Don'tcall me, I'll call you back!”,在Android中,不同组件之间的调用往往是基于消息触发,而不是简单的事件调用。在Android中,广播机制也很好的贯彻了这个思想。下面这个程序,将会演示如何利用BroadcastReceiver来实现Activity和Service的开机自启动。
实现原理:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为android.intent.action.BOOT_COMPLETED。所以,只要在BroadcastReceiver接收到该消息时,创建并启动相应的Activity和Service即可实现。
在该程序中,将创建一个BroadcastReceiver类BootBroadcastReceiver、一个Activity类StartOnBootActivity、一个Service类StartOnBootService。程序的示例程序的代码如下,重点代码的注解见代码中注释部分:
(1)配置文件“AndroidManifest.xml”
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="lulicheng.android.onboot" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" > </uses-permission> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".StartOnBootActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".StartOnBootService" > </service> <receiver android:name=".BootBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="lulicheng.android.onboot" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" > </uses-permission> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".StartOnBootActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".StartOnBootService" > </service> <receiver android:name=".BootBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest>
在该配置文件中,配置了各个组件的基本参数,在使用权限中需要加入“<uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED" >”权限,另外还有一点比较重要的就是在BootBroadcastReceiver中添加intent-filter,如此一来BootBroadcastReceiver的onReceiver方法才能被触发。
(2)广播监听类“BootBroadcastReceiver”
package lulicheng.android.onboot; 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) { // 当收听到的事件是“BOOT_COMPLETED”时,就创建并启动相应的Activity和Service if (intent.getAction().equals(ACTION)) { // 开机启动的Activity Intent activityIntent = new Intent(context, StartOnBootActivity.class); activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 启动Activity context.startActivity(activityIntent); // 开机启动的Service Intent serviceIntent = new Intent(context, StartOnBootService.class); // 启动Service context.startService(serviceIntent); } } } (3)“StartOnBootActivity” package lulicheng.android.onboot; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class StartOnBootActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置Activity的显示内容为一个文本域 TextView aTextView = new TextView(this); aTextView.setText("Wow!I started after cellphone boot."); setContentView(aTextView); } } (4)“StartOnBootService” package lulicheng.android.onboot; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class StartOnBootService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); // Service被启动时,将会有弹出消息提示[MyService onStart] Toast.makeText(this, "[MyService onStart]", Toast.LENGTH_LONG).show(); } } package lulicheng.android.onboot; 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) { // 当收听到的事件是“BOOT_COMPLETED”时,就创建并启动相应的Activity和Service if (intent.getAction().equals(ACTION)) { // 开机启动的Activity Intent activityIntent = new Intent(context, StartOnBootActivity.class); activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 启动Activity context.startActivity(activityIntent); // 开机启动的Service Intent serviceIntent = new Intent(context, StartOnBootService.class); // 启动Service context.startService(serviceIntent); } } } (3)“StartOnBootActivity” package lulicheng.android.onboot; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class StartOnBootActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置Activity的显示内容为一个文本域 TextView aTextView = new TextView(this); aTextView.setText("Wow!I started after cellphone boot."); setContentView(aTextView); } } (4)“StartOnBootService” package lulicheng.android.onboot; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class StartOnBootService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); // Service被启动时,将会有弹出消息提示[MyService onStart] Toast.makeText(this, "[MyService onStart]", Toast.LENGTH_LONG).show(); } }
程序实现后,在手机或者模拟器上面安装,然后重启机器,就能在开机进入系统之后看到该Activity界面以及Service的弹出消息。程序的运行截屏如下图: