Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of dynamic updating of android

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "android dynamic update method". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "android dynamic update method" can help you solve the problem.

Principle 1 Activity-alias

In AndroidMainifest, there are two attributes:

//Determines the Activity android.intent.action.MAIN that the app *** starts//Determines whether the app appears in the app list android.intent.category.LAUNCHER

In addition, there is an activity-alias attribute, this attribute can be used to create a number of different entries, I believe that developers who have done system Setting and Launcher development should have seen a lot in the system source code.

Principle 2 PM.setComponentEnabledSetting

PackageManager is a large command class, you can manage all system components, of course, if Root, you can also manage all components of other apps, some system optimization tools are through this way to disable some background Service.

How to use it is exceptionally simple:

private void enableComponent(ComponentName componentName) { mPm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } private void disableComponent(ComponentName componentName) { mPm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); }

According to PackageManager.COMPONENT_ENABLED_STATE_ENABLED and PackageManager.COMPONENT_ENABLED_STATE_DISABLED and the corresponding Component Name, you can control whether a component is enabled or not.

Dynamic Change Icon

With the above two principles, to achieve dynamic replacement of Icon, only the problem of thinking remains.

First, we create an Activity as the default entry with the default image, then create an activity-alias with double 11 pointing to the default Activity with the double 11 image, then create an activity-alias with double 12 pointing to the default Activity with the double 12 image…etc.

Wait, there is a problem with this, that is, this will show 3 entries on the Launcher, so by default we will disable these activity-aliases first, and then enable them when they are needed.

public class MainActivity extends AppCompatActivity { private ComponentName mDefault; private ComponentName mDouble11; private ComponentName mDouble12; private PackageManager mPm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDefault = getComponentName(); mDouble11 = new ComponentName( getBaseContext(), "com.xys.changeicon.Test11"); mDouble12 = new ComponentName( getBaseContext(), "com.xys.changeicon.Test12"); mPm = getApplicationContext().getPackageManager(); } public void changeIcon11(View view) { disableComponent(mDefault); disableComponent(mDouble12); enableComponent(mDouble11); } public void changeIcon12(View view) { disableComponent(mDefault); disableComponent(mDouble11); enableComponent(mDouble12); } private void enableComponent(ComponentName componentName) { mPm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } private void disableComponent(ComponentName componentName) { mPm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); } }

OK, after disabling the default Activity, enable the activity-alias of Double 11, the result is still pointing to the default Activity, but the icon has changed.

Depending on the ROM, after disabling the component, wait a while and Launcher will refresh the icon automatically.

See below for effect.

About "android dynamic update method" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report