Android動態更換桌面圖標

動態更換APP 桌面icon的引述

activity組件及定義“同盟”組件activity-alias;

PackageManager類進行啟用/禁用組件;

PackageInfo的簡介;

新名詞記錄{PackageInfo:Androidmanifest.xml文件描述類}

使用Activity-alias


<activity

? ? android:name="com.aliasicon.MainActivity"

? ? android:enabled="false"

? ? android:exported="true"

? ? >

? ? <intent-filter>

? ? ? ? <action android:name="android.intent.action.MAIN"/>

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

? ? </intent-filter>

</activity>

<activity-alias

? ? android:name="com.android.simple.activity.HeadPageGenearlActivity"

? ? android:configChanges="orientation|keyboardHidden|fontScale|screenSize"

? ? android:enabled="true"

? ? android:exported="true"

? ? android:icon="@drawable/icon"

? ? android:label="@string/app_general_name"

? ? android:screenOrientation="portrait"

? ? android:targetActivity=".MainActivity">

? ? <intent-filter>

? ? ? ? <action android:name="android.intent.action.MAIN"/>

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

? ? </intent-filter>

</activity-alias>

<activity-alias

? ? android:name="com.android.simple.activity.HeadPageLicaiActivity"

? ? android:configChanges="orientation|keyboardHidden|fontScale|screenSize"

? ? android:enabled="false"

? ? android:exported="true"

? ? android:icon="@drawable/icon_licai"

? ? android:label="@string/app_licai_name"

? ? android:screenOrientation="portrait"

? ? android:targetActivity=".MainActivity">

? ? <intent-filter>

? ? ? ? <action android:name="android.intent.action.MAIN"/>

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

? ? </intent-filter>

</activity-alias>

String systemPath = "com.aliasicon.MainActivity";

String springPath = "com.android.simple.activity.CommHeadPageAliasActivity";

String licaiPath = "com.android.simple.activity.CommHeadPageLicaiActivity";

String genearlPath = "com.android.simple.activity.CommHeadPageGenearlActivity";

String finalPath = "";

ComponentName genearlComponent;

ComponentName licaiComponent;

ComponentName springComponent;

@Override

protected void onCreate(Bundle savedInstanceState) {

? ? super.onCreate(savedInstanceState);

? ? setContentView(R.layout.activity_main);

? ? genearlComponent = new ComponentName(getApplication(), genearlPath);

? ? licaiComponent = new ComponentName(getApplication(), licaiPath);

? ? springComponent = new ComponentName(getApplication(), springPath);

//第一個默認按鈕

? ? Button btnDefault = (Button) findViewById(R.id.ToDefault);

? ? btnDefault.setOnClickListener(new View.OnClickListener() {

? ? ? ? @Override

? ? ? ? public void onClick(View v) {

? ? ? ? ? ? disableComponent(MainActivity.this, licaiComponent);

? ? ? ? ? ? disableComponent(MainActivity.this, springComponent);

? ? ? ? ? ? enableComponent(MainActivity.this, genearlComponent);

? ? ? ? }

? ? });

? ? //理財按鈕

? ? Button btnLiCai = (Button) findViewById(R.id.ToLiCai);

? ? btnLiCai.setOnClickListener(new View.OnClickListener() {

? ? ? ? @Override

? ? ? ? public void onClick(View v) {

? ? ? ? ? ? disableComponent(MainActivity.this, genearlComponent);

? ? ? ? ? ? disableComponent(MainActivity.this, springComponent);

? ? ? ? ? ? enableComponent(MainActivity.this, licaiComponent);

? ? ? ? }

? ? });

? ? //春節按鈕

? ? Button btnSpring = (Button) findViewById(R.id.ToSpring);

? ? btnSpring.setOnClickListener(new View.OnClickListener() {

? ? ? ? @Override

? ? ? ? public void onClick(View v) {

? ? ? ? ? ? disableComponent(MainActivity.this, genearlComponent);

? ? ? ? ? ? disableComponent(MainActivity.this, licaiComponent);

? ? ? ? ? ? enableComponent(MainActivity.this, springComponent);

? ? ? ? }

? ? });

}

/**

* 啟用組件 *

* @param componentName

* 重要方法

*/

private void enableComponent(Activity activity, ComponentName componentName) {

? ? PackageManager pm = activity.getPackageManager();

? ? int state = pm.getComponentEnabledSetting(componentName);

? ? if (PackageManager.COMPONENT_ENABLED_STATE_ENABLED == state) {

? ? ? ? //已經啟用

? ? ? ? return;

? ? }

? ? pm.setComponentEnabledSetting(componentName,

? ? ? ? ? ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED,

? ? ? ? ? ? PackageManager.DONT_KILL_APP);

}

/**

* 禁用組件 *

* @param componentName

* 重要方法

*/

private void disableComponent(Activity activity, ComponentName componentName) {

? ? PackageManager pm = activity.getPackageManager();

? ? int state = pm.getComponentEnabledSetting(componentName);

? ? if (PackageManager.COMPONENT_ENABLED_STATE_DISABLED == state) {

? ? ? ? //已經禁用

? ? ? ? return;

? ? }

? ? pm.setComponentEnabledSetting(componentName,

? ? ? ? ? ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED,

? ? ? ? ? ? PackageManager.DONT_KILL_APP);

}

public void saveData(String savePath) {

? ? SharedPreferences sp = getSharedPreferences("Icon", Context.MODE_PRIVATE);

? ? //獲取到edit對象

? ? SharedPreferences.Editor edit = sp.edit();

? ? //通過editor對象寫入數據

? ? edit.putString("Value", savePath);

? ? //提交數據存入到xml文件中

? ? edit.commit();

}

public String getData() {

? ? SharedPreferences sp = getSharedPreferences("Icon", Context.MODE_PRIVATE);

? ? return sp.getString("Value", "");

}

其他

? ? ? 通過上面的程序能夠實現卻換圖標;如果代碼里通過節假日的時間進行控制,則客戶打開app會自動的切換圖標;

? ? ? 是不是很神奇?先別高興太早。

App的覆蓋

? ? ? 任何App都會更新,進行覆蓋安裝。覆蓋安裝的Manifest的配置不當導致出現難以挽回的問題。

? ? ? 1、從上面的xml里配置可見,一個Activity和兩個Activity-alisa的配置情況:

Activity的android:enabled="false"

第一個Activity-alias的android:enabled="true"

第二個Activity-alias的android:enabled="false"

? ? ? ? ? ? 所以App打開,桌面上默認的就是使用第一個Activity-alias的名字和圖標。

? ? ? 2、如果新版本的配置還是按照這個配置(即使添加了新的alias,只要默認的Activity-alias不發生變化)客戶端額升級覆蓋安裝,是不會出現錯誤的。

3、我們稱之為“方案一”

桌面上出現兩個圖標的問題

? ? ? 1、如果新版本把Activity的enable設置為true(方案二),則桌面上除了原來的Activity-alias圖標之外,還擁有了該Activity的圖標,即有了兩個圖標;

Activity的android:enabled="true"

第一個Activity-alias的android:enabled="false"

第二個Activity-alias的android:enabled="false"

? ? ? 3、如果試圖使用使用上面的代碼里的disableComponent方法或者android:enabled="false"隱藏顯示的Activity-alias圖標,則導致兩個圖標都消失;

桌面上圖標消失的問題

? ? ? 1、方案二會出現兩個圖標,如果再使用方案一進行覆蓋,則兩個圖標都消失。

? ? ? 2、如果再次使用方案二進行覆蓋,則圖標還是能重新顯示出來的。

總結

? ? ? 1、如果使用方案一進行覆蓋安裝,不管圖標怎么動態的變換,再使用方案一進行覆蓋安裝,是正常的;

? ? ? 2、如果使用方案二進行覆蓋安裝,不管配置的圖標是什么樣的,再使用方案二進行覆蓋安裝,也是正常的;

? ? ? 3、如果使用方案一進行覆蓋安裝,如果圖標沒有動態切換的情況下,再使用方案二進行覆蓋安裝,也是正常的;

? ? ??4、如果使用方案一進行覆蓋安裝,如果圖標已經經過動態切換的情況下,再使用方案二進行覆蓋安裝,會出現雙圖標的;

? ? ??5、如果使用方案一進行覆蓋安裝,如果圖標已經經過動態切換的情況下,再使用方案二進行覆蓋安裝,會出現雙圖標的;如果在進行使用代碼試圖隱藏其中一個,則兩個圖標都消失;

? ? ? 6、如果使用方案一進行覆蓋安裝,如果圖標沒有動態切換的情況下,再使用方案二進行覆蓋安裝,也是正常的,再使用方案一進行覆蓋安裝,也是正常的;

最終方案(方案一)

? ? ? 采用方案一的方案,即通過代碼動態變動圖標,一定要注意以下事項:

? ? ? 1、Activity的android:enabled="false"

? ? ? ? ? ? 如果設置為true了,則會出現雙圖標;

? ? ? 2、Activity-alias的android:enabled="true"的默認顯示的項不要中途進行變動,如果確實需要使用新的默認值,則使用代碼進行動態變換;

? ? ? 3、Activity-alias的android:enabled="true"的不要設置為多個,否則會出現多個圖標,如果試圖通過代碼進行隱藏其中的一個或者幾個,可能會出現圖標消失的情況。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。