1. 程式人生 > >應用開機啟動及相關文件翻譯

應用開機啟動及相關文件翻譯

一、應用開機啟動

1、新增許可權:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2、廣播接收器靜態註冊且增加相應的action

<receiver android:name=".BootCompleteReceiver" >

    <intent-filter>

         <action android:name="android.intent.action.BOOT_COMPLETED"/>

    </intent-filter>

</receiver>

3、在receiver中啟動應用需新增標識

Intent bootStartIntent = new Intent(context,MainActivity.class);

bootStartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(bootStartIntent);

二、使用adb傳送BOOT_COMPLETED

我們可以通過

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

命令傳送BOOT_COMPLETED廣播,而不用重啟測試機或模擬器來測試BOOT_COMPLETED廣播,這條命令可以更精確的傳送到某個package,如下:

adb shell am broadcast -aandroid.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -npackage_name/class_name

三、幾種不同方式安裝應用的測試

a)       使用U盤安裝簽名的普通應用

結論:此開機啟動是成功的,但應用需要執行一次程式,否則收不到開機廣播!

b)     使用IDE執行應用到裝置上

結論:此開機啟動是成功的

c)     應用放到system/app目錄中成為系統級應用

結論:此開機啟動是成功的,無需應用啟動過一次。

四、自啟動失敗的原因

接收不到BOOT_COMPLETED廣播可能的原因

(1)、BOOT_COMPLETED對應的action和uses-permission沒有一起新增

(2)、應用安裝到了sd卡內,安裝在sd卡內的應用是收不到BOOT_COMPLETED廣播的

(3)、系統開啟了Fast Boot模式,這種模式下系統啟動並不會傳送BOOT_COMPLETED廣播

(4)、應用程式安裝後重來沒有啟動過,這種情況下應用程式接收不到任何廣播,包括BOOT_COMPLETED、ACTION_PACKAGE_ADDED、CONNECTIVITY_ACTION等等。

Android3.1之後,系統為了加強了安全性控制,應用程式安裝後或是(設定)應用管理中被強制關閉後處於stopped狀態,在這種狀態下接收不到任何廣播,除非廣播帶有FLAG_INCLUDE_STOPPED_PACKAGES標誌,而預設所有系統廣播都是FLAG_EXCLUDE_STOPPED_PACKAGES的,所以就沒法通過系統廣播自啟動了。所以Android3.1之後

(1)、應用程式無法在安裝後自己啟動

(2)、沒有ui的程式必須通過其他應用啟用才能啟動,如它的Activity、Service、Content Provider被其他應用呼叫。

存在一種例外,就是應用程式被adb push you.apk/system/app/下是會自動啟動的,不處於stopped狀態。

具體詳見:

http://developer.android.com/about/versions/android-3.1.html#launchcontrols

http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html

五、相關文件翻譯

Launchcontrols on stopped applications

有關停止的應用的啟動控制

Startingfrom Android 3.1, the system's package manager keeps track of applications thatare in a stopped state and provides a means of controlling their launch frombackground processes and other applications.

從Android3.1開始,系統包管理器儲存停止狀態應用的痕跡,並提供一種方式控制它們從後臺程序和其他應用啟動。

Notethat an application's stopped state is not the same as an Activity's stoppedstate. The system manages those two stopped states separately.

注意應用的停止狀態和活動的停止狀態是不同的。系統分別管理這兩種停止狀態。

Theplatform defines two new intent flags that let a sender specify whether theIntent should be allowed to activate components in stopped application.

平臺定義了兩個新的意圖示識,讓傳送方指定意圖是否允許啟用停止的應用中的元件。

FLAG_INCLUDE_STOPPED_PACKAGES— Include intent filters of stopped applications in the list of potentialtargets to resolve against.

FLAG_INCLUDE_STOPPED_PACKAGES-針對解決包含停止的應用的意圖過濾器在潛在目標名單中。

FLAG_EXCLUDE_STOPPED_PACKAGES— Exclude intent filters of stopped applications from the list of potentialtargets.

FLAG_EXCLUDE_STOPPED_PACKAGES-排除停止的應用的意圖過濾器在潛在目標名單中。

Whenneither or both of these flags is defined in an intent, the default behavior isto include filters of stopped applications in the list of potential targets.

當這些標誌沒有或兩者都被定義在一個意圖中,則預設行為是包括停止的應用的過濾器在潛在目標名單中。

Notethat the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. Itdoes this to prevent broadcasts from background services from inadvertently orunnecessarily launching components of stoppped applications. A backgroundservice or application can override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowedto activate stopped applications.

需要注意的是該系統增加FLAG_EXCLUDE_STOPPED_PACKAGES到所有的廣播意圖。它這樣做是為了防止來自後臺服務的廣播無意或不必要的啟動停止的應用程式的元件。後臺服務或應用程式可以通過新增FLAG_INCLUDE_STOPPED_PACKAGES標誌覆蓋此行為,以廣播允許啟用停止的應用程式的意圖。

Applicationsare in a stopped state when they are first installed but are not yet launchedand when they are manually stopped by the user (in Manage Applications).

當應用程式第一次安裝且沒有被啟動過或者手動被使用者停止(在管理應用程式中)時,應用程式處於被停止狀態。