android開機廣播;代碼以下:html
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * 開機廣播 * @author UNIVERSE * */ public class BootCompletedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.e("BootCompletedReceiver", "系統啓動完畢"); } }
AndroidManifest.xml文件中進行註冊:
<receiver android:name="com.android.callrecords.receiver.BootCompletedReceiver"> <intent-filter android:priority="1000"> <action android:name="android.intent.action.BOOT_COMPLETED"/> <category android:name="android.intent.category.HOME" /> </intent-filter> </receiver>
同時應添加所須要的權限:java
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> android
既然Android系統在啓動完畢後會發送廣播,在系統關閉時是否也有對應的廣播呢?經過查詢幫助文檔,找到了系統關閉的廣播:ide
關機廣播:spa
<receiver android:name=".ShutdownReceiver"> code
<intent-filter> orm
<action android:name="android.intent.action.ACTION_SHUTDOWN"/> xml
</intent-filter> htm
</receiver> 文檔
不太小米手機須要手動開啓自動啓動選項才能接收到廣播,否則接受不到