Android應用程序自啓動,已用於售賣機開機自啓動程序

第一步:在你的App中設置一個廣播
在這裏插入圖片描述
【MyReceiver.java】

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction().toString();
        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
                Intent intents = new Intent(context, WelcomeActivity.class);
                intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intents);
            }
        }
    }
}

在這裏插入圖片描述
第二部:在 【AndroidManifest.xml】文件中註冊剛寫的服務

在這裏插入圖片描述

<receiver android:name=".recevice.MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </receiver>

第三步:在 【AndroidManifest.xml】文件中申請權限
在這裏插入圖片描述

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
發佈了39 篇原創文章 · 獲贊 11 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章