[Android] 開機啟動與解鎖啟動

Android 使用 BroadcastReceiver 監聽 ACTION_BOOT_COMPLETED(開機)和 ACTION_USER_PRESENT(解鎖)事件的程式碼範例,須在 AndroidManifest.xml 中宣告。

BroadcastReceiver 開機啟動與解鎖啟動

主要是這兩個 intent:

Intent.ACTION_BOOT_COMPLETED //開機

Intent.ACTION_USER_PRESENT //解鎖

只要建立下面的 Receiver

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class MyReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(final Context context, final Intent intent) {
		final String action = intent.getAction();
		if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
      // TODO 開機啟動
		} else if (Intent.ACTION_USER_PRESENT.equals(action)) {
			// TODO 解鎖啟動
		}
	}   
}

並且在 AndroidManifest.xml 裡宣告就可以了

comments powered by Disqus
Powered by Hugo. Theme Stack. All Rights Reserved.