標籤:android java broadcastreceiver filter
上一節已經介紹過BroadcastReceiver實現即時監聽電量的功能,這節就來介紹一下如果實現開機自動啟動的功能,這個比監聽電量還簡單不少
(1)在資訊清單檔註冊許可權
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
(2)實現 BroadcastReceiver介面
package com.example.g04_broadcastreciver04;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class BootComplete extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stub Intent intent2=new Intent(context,MainActivity.class); intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent2); }}
(3)在資訊清單檔註冊Receiver
<receiver android:name="com.example.g04_broadcastreciver04.BootComplete" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" > </action> <category android:name="android.intent.category.HOME" > </category> </intent-filter> </receiver>
Android開發之使用BroadcastReceiver實現開機自動啟動(原始碼分享)