赞
踩
问题描述:支付宝、美团、淘宝之类的在某些节日的时候,桌面图标会进行自动进行变化,但是并没有进行APP更新之类的,想做这样的效果,怎么弄?
解决方案:
(1)使用activity-alias来定义多个程序启动入口。
(2)调用PackageManager类中的相关方法进行处理。(从哪一个入口启动程序)
相关代码:
(1)在AndroidManifest.xml配置文件中添加多个程序启动入口
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity-alias android:name=".IconType_1" android:targetActivity=".MainActivity" android:enabled="true" android:label="icon_1" android:icon="@drawable/icon_1"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias> <activity-alias android:name=".IconType_2" android:targetActivity=".MainActivity" android:enabled="false" android:label="icon_2" android:icon="@drawable/icon_2"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias>
(2)在相应的MainActivity.java文件中处理相应操作。
Package管理工具类(已封装)
/** * Package管理器工具类 */ public class PackageManagerUtil { private PackageManager mPackageManager; public PackageManagerUtil(PackageManager mPackageManager) { this.mPackageManager = mPackageManager; } /** * 启动组件 * @param componentName 组件名 */ public void enableComponent(ComponentName componentName) { //此方法用以启用和禁用组件,会覆盖AndroidManifest文件下定义的属性 mPackageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } /** * 禁用组件 * @param componentName 组件名 */ public void disableComponent(ComponentName componentName) { //此方法用以启用和禁用组件,会覆盖AndroidManifest文件下定义的属性 mPackageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); } }
如何利用工具类进行操作?
public class MainActivity extends AppCompatActivity { private Context mContext; private Button btnSwitchIcon_1, btnSwitchIcon_2; private PackageManagerUtil packageManagerUtil; //默认组件 private ComponentName componentNameDefault; private ComponentName componentName1; private ComponentName componentName2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; //获取到包管理类实例 packageManagerUtil = new PackageManagerUtil(getPackageManager()); //得到此activity的全限定名 componentNameDefault = getComponentName(); //根据全限定名创建一个组件,即activity-alias 节点下的name:IconType_1 对应的组件 componentName1 = new ComponentName(getBaseContext(), "com.deepreality.dynamicicondemo.IconType_1"); componentName2 = new ComponentName(getBaseContext(), "com.deepreality.dynamicicondemo.IconType_2"); btnSwitchIcon_1 = findViewById(R.id.Main_btnSwitchIcon_1); btnSwitchIcon_2 = findViewById(R.id.Main_btnSwitchIcon_2); btnSwitchIcon_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { enableComponentName1(); Toast.makeText(mContext, "图标替换成功!", Toast.LENGTH_SHORT).show(); } }); btnSwitchIcon_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { enableComponentName2(); Toast.makeText(mContext, "图标替换成功!", Toast.LENGTH_SHORT).show(); } }); } /** * 设置第icon1图标生效 */ private void enableComponentName1() { packageManagerUtil.disableComponent(componentNameDefault); packageManagerUtil.disableComponent(componentName2); packageManagerUtil.enableComponent(componentName1); } /** * 设置第icon2图标生效 */ private void enableComponentName2() { packageManagerUtil.disableComponent(componentNameDefault); packageManagerUtil.disableComponent(componentName1); packageManagerUtil.enableComponent(componentName2); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。