Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

android - How to enable and disable a component?

My initial question was basically something along the lines of this: Clearing and setting the default home application

That question was answered to my satisfaction, however, the thing I'm having difficulty understanding in the answer is how do you enable and then disable a component from the manifest in the java code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By using package manager you can enable or disable component declared in manifest file There are two flag PackageManager.COMPONENT_ENABLED_STATE_DISABLED for disable component and PackageManager.COMPONENT_ENABLED_STATE_ENABLED for enable component.

PackageManager pm = getApplicationContext().getPackageManager();
ComponentName componentName = new ComponentName("com.app",
    ".broadcast_receivers.OnNetworkChangedReceiver");
pm.setComponentEnabledSetting(componentName,
    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
    PackageManager.DONT_KILL_APP);

Android manifest:

<receiver
  android:name=".broadcast_receivers.OnNetworkChangedReceiver"
  android:enabled="true"
>
  <intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  </intent-filter>
</receiver>

Official documentation: setComponentEnabledSetting


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...