(有两个图标同时存在)
一,通过快捷方式形式,
// 需添加权限<uses-permission android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>
public static void
createShrotcut(Activity activity,
int
iconResId,
int
nameResId){
Intent intent = new
Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Intent.ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(activity,
iconResId);//实现了parcelable接口
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,nameResId);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(activity.getApplicationContext(),activity.getClass()));
intent.putExtra("duplicate",false);
activity.sendBroadcast(intent);
}
二,修改启动方式,(也可通过指定不同 :process.///)
<intent-filter>
<action
android:name="android.intent.action.MAIN"
/>
<category
android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
三,通过别名 <activity-alias>
(只有一个图标)(动态更换图标))
代码如下:
public void
updateIcon(View v){
PackageManager pm = getPackageManager();
ComponentName componentName = getComponentName();
Log.d("componentName()",
componentName.getClassName());
pm.setComponentEnabledSetting(getComponentName(),PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new
ComponentName(getApplicationContext(),"com.example.myapplication.alias1"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
}
完整清单文件如下:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="原图标"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
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="alias2"
android:enabled="false"
android:icon="@mipmap/ic_launcher2"
android:label="新图标"
android:targetActivity=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN"
/>
<category
android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity-alias>
实现多程序入口,会有BUG,请谨慎使用