Android7.1.1開機自動啟動配置
在AndroidManifest.xml檔案中新增許可權
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
在AndroidManifest.xml檔案中註冊接收廣播配置, 新增到manifest > application節點下
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
開機啟動執行程式碼
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* 接收開機廣播
*/
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}