Android開機向導的實現
在android TV第一次上電時,會進行一個國家和語言及搜臺的選擇,實現的原理是:
android:excludeFromRecents="true"
android:launchMode="singleInstance">
<intent-filter android:priority="1">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
寫一個單獨的apk實現相應的功能,把這個單獨的apk設置成Launcher:
在AndroidManifest.xml中,設置:
<application>
<activity android:name="DefaultActivity"android:excludeFromRecents="true"
android:launchMode="singleInstance">
<intent-filter android:priority="1">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
那么開機時,理論上會有兩個Launcher啟動,用戶可以選擇,但這里設置了一個屬性:intent-filter android:priority="1"
而默認的Launcher沒設置這個屬性:
<activity
android:name ="com.haier.haierlauncher2.LauncherActivity"
android:label ="@string/app_name"
android:launchMode ="singleInstance"
android:theme ="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" >
< intent-filter>
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.HOME" />
< category android:name ="android.intent.category.DEFAULT" />
< category android:name ="android.intent.category.MONKEY" />
</ intent-filter>
</ activity>
沒有設置這個屬性值,默認為0,:intent-filter android:priority="0"
這個屬性用于給過濾器設置一個優先級。其父組件能夠通過優先級來依次處理過濾器所描述類型的Intent對象。這個屬性對Activity和Broadcast Receiver對象有意義:
1. 它提供了Activity能夠如何響應跟過濾器匹配的Intent對象請求的信息(相對與其他的也能響應這個Intent請求的Activity)。當一個Intent對象請求能夠被多個帶有不同優先級的Activity處理時,Android系統只考慮把高優先級的Intent過濾器作為潛在的 Intent對象請求目標。
2. 它控制了Broadcast Receiver執行接收廣播消息的順序。高優先級的過濾器會優先調用。(這個順序只應用與同步消息,對于異步消息會忽略優先級的設置。)
只有真正想要給接收廣播消息的過濾器施加一個特定順序,或者要強制Android來應用另外一個Activity,才會使用這個屬性。
這個屬性值必須是一個整數,如:100,數字越大,優先級越高。
那么第一次上電,優先啟動的是這個Launcher,
設置完成之后,以后這個Launcher不再啟動,可以這樣做:
private void finishSetupWizard() {
// Add a persistent setting to allow other apps to know the device has been provisioned.
Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED , 1);
Settings.Secure.putInt(getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, 1);
// remove this activity from the package manager.
PackageManager pm = getPackageManager();
ComponentName name = new ComponentName(this, DefaultActivity.class);
pm. setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP );
finish();
}
在用到組件時,有時候我們可能暫時性的不使用組件,但又不想把組件kill掉,比如創建了一個broadcastReceiver廣播監聽器,用來想監聽第一次開機啟動后獲得系統的許多相關信息,并保存在文件中,這樣以后每次開機啟動就不需要再去啟動該服務了,也就是說如果沒有把receiver關閉掉,就算是不做數據處理,但程序卻還一直在后臺運行會消耗電量和內存,這時候就需要把這個receiver給關閉掉。
如何關閉組件?
關閉組件其實并不難,只要創建packageManager對象和ComponentName對象,并調用packageManager對象的setComponentEnabledSetting方法。
public void setComponentEnabledSetting (ComponentName componentName, int newState, int flags)
componentName:組件名稱
newState:組件新的狀態,可以設置三個值,分別是如下:
不可用狀態:COMPONENT_ENABLED_STATE_DISABLED
可用狀態:COMPONENT_ENABLED_STATE_ENABLED
默認狀態:COMPONENT_ENABLED_STATE_DEFAULT
flags:行為標簽,值可以是DONT_KILL_APP或者0。 0說明殺死包含該組件的app
本文由用戶 mip33 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!