Android開機自啟動應用


問題場景

最近開發一個展示類應用項目,展示設備為若干個24小時運行的Android廣告機。考慮到停電的情況該應用需要開機自啟動。

背景知識

  • 當Android啟動時,會發出一個系統廣播,內容為ACTION_BOOT_COMPLETED,它的字符串常量表示為 android.intent.action.BOOT_COMPLETED。

  • android開發中的基本概念:Activity。Activity簡單的理解為android的視圖,承載着android的人機交互。一個應用程序可以有多個Activity,其中有一個Activity為應用程序啟動時最先啟動的。 該Activity在AndroidManifest.xml中的具體形式如下。intent-filter中兩項android.intent.action.MAIN 和 android.intent.category.LAUNCHER表示該activity為應用程序啟動主界面

到這里解決問題的思路就完整了,我們監聽ACTION_BOOT_COMPLETED廣播,並在監聽邏輯中啟動應用的對應的Main Activity。

方案:

  • AndroidMainfest.xml中添加開機啟動權限

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  • 創建一個廣播接收類

package io.dcloud.yourapp;  //要修改成工程的包名 import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import io.dcloud.PandoraEntry;  //H5+SDK public class BootBroadcastReceiver extends BroadcastReceiver {  

    static final String action_boot="android.intent.action.BOOT_COMPLETED";  

    @Override  
    public void onReceive(Context context, Intent intent) {  

        if (intent.getAction().equals(action_boot)){  

            // 注意H5+SDK的Main Activity為PandoraEntry(見AndroidMainfest.xml)  
            Intent bootMainIntent = new Intent(context, PandoraEntry.class);  

            // 這里必須為FLAG_ACTIVITY_NEW_TASK  
            bootMainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  

            context.startActivity(bootMainIntent);  
        }  

    }  
}
  • 在AndroidMainfest.xml中注冊該廣播接收類
    <!--開機自啟動-->  
    <receiver android:name=".BootBroadcastReceiver">  
    <intent-filter>  
         <action android:name="android.intent.action.BOOT_COMPLETED" />  
         <category android:name="android.intent.category.LAUNCHER"></category>  
    </intent-filter>  
    </receiver>

     

注意事項

  • 請注意BootBroadcastReceiver的命名空間,要保證AndroidMainfest.xml中receiver可以找的到我們創建的BootBroadcastReceiver類。

  • 應用程序必須在Android中啟動一次,下次才可以開機啟動。

 

原文地址:https://ask.dcloud.net.cn/article/id-689__page-2

 

下面是我寫的代碼。使用H5+SDK離線打包,在Android一體機中實驗成功。

AndroidMainfest.xml完整配置

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
 <application
        android:name="io.dcloud.application.DCloudApplication"
        android:allowClearUserData="true"
        android:exported="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:largeHeap="true">

        <activity
            android:name="io.dcloud.PandoraEntry"
            android:configChanges="orientation|keyboardHidden|keyboard|navigation"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:hardwareAccelerated="true"
            android:theme="@style/TranslucentTheme"
            android:screenOrientation="user"
            android:windowSoftInputMode="adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <!--開機自啟動-->
        <receiver android:name=".BootBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>
        </receiver>
</application>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM