1. 程式人生 > >Android-如何設定APP開機啟動(圖文)

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

方案:

第一步:接收“開機完成”廣播:android.intent.action.BOOT_COMPLETED;

第二步:在廣播接收器中啟動HelloApp:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.excample.helloapp">
<!--宣告接收啟動完成廣播的許可權-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".BootCompleteReceiver" android:enabled="true" android
:exported="true"> <intent-filter android:priority="1000"> <!--.接收啟動完成的廣播--> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application> </manifest>

廣播接收器:

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);
}
    }

}

------------------------------------------------------------------------------------------------------------------------

圖文詳解:

   首先,你得有一個APP!

建立HelloApp:

1.新建專案,取名HelloApp


2.下一步(保持預設)


3.下一步(保持預設)


4.下一步(保持預設)


5.修改圖示為”Project"(可選)


6.Project圖示效果:


7.執行


8.執行效果:


設定HelloApp開機啟動:

1.


2.


3.在廣播接收器中啟動HelloApp:

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);
}
    }

}

4.修改AndroidManifest.xml註冊廣播過濾器:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.excample.helloapp">
<!--宣告接收啟動完成廣播的許可權-->
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />    <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>        </activity>
        <receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
            <intent-filter>
<!--.接收啟動完成的廣播-->
<action android:name="android.intent.action.BOOT_COMPLETED"/>            </intent-filter>
        </receiver>
    </application>
</manifest>

5.將編譯出來的apk放到/system/app/下面,然後重啟


6.效果


(完)