Android-如何設置APP開機啟動(圖文)

Android-如何設置APP開機啟動(圖文)

首先需要了解下Android系統的廣播機制,不懂的同學可以自行百度下。

百度得知,當Android系統開機時,會發生一個系統廣播,內容是android.intent.action.BOOT_COMPLETED

那麼思路就來了:

1、註冊廣播

2、監聽開機廣播,打開本應用APP

一、註冊廣播

1、添加權限


<uses-permission>

2、註冊廣播接收者

<receiver> android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>

<action>
/<intent-filter>
/<receiver>


二、監聽廣播事件

package com.excample.helloapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class BootCompleteReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
Intent thisIntent = new Intent(context, MainActivity.class);
thisIntent.setAction("android.intent.action.MAIN");
thisIntent.addCategory("android.intent.category.LAUNCHER");
thisIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(thisIntent);
}
}

}

Github代碼:https://github.com/menghaocheng/HelloApp


分享到:


相關文章: