1 通过禁用组件和启用组件更换
1.1 配置Activity
<activity
android:name=".tztCommHeadPageActivity"
android:configChanges="orientation|keyboardHidden|fontScale|screenSize"
android:label="@string/tzt_app_name"
android:screenOrientation="portrait"
android:theme="@style/tzt_HeadPage_Theme.White">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
1.2 配置多个icon
Android可以配置多个activity-alias标签 来为指定activity设置多个引用
targetActivity指定点击图标后响应的Activity, name属性为activity-alias唯一标识,java代码中通过该参数获得该activity引用
<activity
android:name=".activity.SplashActivity"
android:hardwareAccelerated="true"
android:screenOrientation="portrait"
android:theme="@style/AppThemeTransparentBackground">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
<activity-alias
android:name=".activity.Test11"
android:enabled="true"
android:icon="@drawable/ic_au"
android:label="双11"
android:targetActivity=".activity.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity-alias>
1.3 changeIcon
利用PackageManager.setComponentEnabledSetting
/**
* 启用组件 *
* @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);
}
然后调用方法就可以了
private void initLauncherIcon() {
ComponentName mDefault =getComponentName();
ComponentName mEday11 = new ComponentName(
getBaseContext(),
"com.xx.xx.activity.Test11");
ComponentName mEday65 = new ComponentName(
getBaseContext(),
"com.xx.xx.activity.Test65");
mPm = getApplicationContext().getPackageManager();
changeIcon65(mDefault, mEday65);
}
public void changeIcon65(ComponentName disable, ComponentName enable) {
disableComponent(disable);
enableComponent(enable);
}
注意:
- PackageManager.DONT_KILL_APP 不会立即杀死进程,所以替换时会等待几秒钟才会生效。
-
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,0);
这里参数传0,可以立即杀死进程,并及时更换icon(一般我们使用这个方式)
2 隐藏app图标以及隐式启动
2.1 隐藏APP桌面图标
在启动的activity的AndroidManifest注册中,添加隐式启动的data:
<data android:host="TestActivity" android:scheme="cn.fly2think.activity" />
在入口Activity处添加申明
<data android:host="TestActivity" android:scheme="cn.fly2think.activity"
tools:ignore="AppLinkUrlError" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="cn.fly2think.activity.TestActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<data android:host="TestActivity" android:scheme="cn.fly2think.activity"
tools:ignore="AppLinkUrlError" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
1.2 通过另一个APP隐式启动
public void starAct() {
Intent intent = new Intent();
ComponentName cn = new ComponentName("com.fly2think.yiilock",
"cn.fly2think.activity.TestActivity");
intent.setComponent(cn);
Uri uri = Uri.parse("cn.fly2think.activity.TestActivity");
intent.setData(uri);
startActivity(intent);
}