Android通過入口別名來動態修改icon
阿新 • • 發佈:2019-01-09
<activity android:name=".AdvertisementActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> </activity> <activity-alias android:name=".default" android:targetActivity=".AdvertisementActivity" android:enabled="true" android:icon="@mipmap/ic_launcher"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias>
<activity-alias android:name=".double11" android:enabled="false" android:icon="@mipmap/liu" android:targetActivity=".AdvertisementActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias> <activity-alias android:name=".double12" android:enabled="false" android:icon="@mipmap/time" android:targetActivity=".AdvertisementActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias>
首先是你的Manifest, 我們使用activity-alias 來定義入口別名,這裡只需要注意android:icon, android:enabled, android:name, android:targetActivity,這四個屬性,分別是圖示,是否啟用入口, 名字, 目標Activity(也就是你要設定別名的Activity)
這裡要注意,你原來的入口Activity要做修改把category屬性去掉,並設定一個預設別名,將android:enabled設定成true,就是我程式碼中標紅的地方。如果不設定的話會接下來會用問題。
public class AdvertisementActivity extends AppCompatActivity{
private ComponentName mDefault;
private ComponentName mDouble11;
private ComponentName mDouble12;
private PackageManager packageManager;
@Override
public void onCreate(Bundle saveInstanceBundle){
super.onCreate(saveInstanceBundle);
setContentView(R.layout.advertisement_activity);
//動態更換圖示
mDefault = getComponentName();
mDouble12 = new ComponentName(getBaseContext(), "com.xxxxx.double12")//全限定包名;
mDouble11 = new ComponentName(getBaseContext(), "com.xxxxxx.double11");
packageManager = getApplicationContext().getPackageManager();
changeIcon12();
}
public void changeIcon12(){
disableComponent(mDefault);
disableComponent(mDouble11);
enableConponent(mDouble12);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addCategory(Intent.CATEGORY_DEFAULT);
List<ResolveInfo> resolves = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo res : resolves) {
if (res.activityInfo != null) {
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.killBackgroundProcesses(res.activityInfo.packageName);
}
}
}
private void enableConponent(ComponentName componentName){
packageManager.setComponentEnabledSetting(componentName
, PackageManager.COMPONENT_ENABLED_STATE_ENABLED
, PackageManager.DONT_KILL_APP);
}
private void disableComponent(ComponentName componentName){
packageManager.setComponentEnabledSetting(componentName
, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
通過PackageManager來設定關閉或開啟那個入口,注意我標紅的地方。若你的APP自動關閉了,請看一下你的Manifest檔案是否設定了預設別名,還有是否入口Activity的
<category android:name="android.intent.category.LAUNCHER" />
是否去掉了