[Android Pro] How Android disables and opens four components (setcomponentenabledsetting)

Source: Internet
Author: User

in the use of components, sometimes we may not use the component temporarily, but do not want to kill the component, such as the creation of a broadcastreceiver broadcast listener, used to listen to the first boot after booting to obtain a lot of information about the system, and saved in the file, In this case, each boot will not need to start the service again, that is, if the receiver is not shut down, even if it does not do data processing, but the program has been running in the background will consume power and memory, this time you need to shut down this receiver.


How do I turn off components?
Closing the component is not difficult, as long as the Packagemanager object and the ComponentName object are created and the Setcomponentenabledsetting method of the Packagemanager object is called.

Public void setcomponentenabledsetting (componentname componentname, int newstate, int flags)

ComponentName: Component Name
NewState: The new state of the component, you can set three values, respectively, are as follows:
Unavailable Status: component_enabled_state_disabled
Available Status: component_enabled_state_enabled
Default state: Component_enabled_state_default
Flags: The behavior tag, the value can be Dont_kill_app or 0. 0 instructions to kill the app that contains the component

receiver (can be a third-party receiver)

Final New componentname (context, receiver to be banned);   Final Packagemanager pm = Context.getpackagemanager ();  Pm.setcomponentenabledsetting (Receiver,packagemanager.component_enabled_state_disabled,packagemanager.dont_kill_app); }

Example two: Hide app icon

If you set an app for Mainactivity, the app will no longer be found in the Launcher program icon

Packagemanager Packagemanager =Getpackagemanager (); ComponentName componentname=NewComponentName ( This, StartActivity.class); intres =packagemanager.getcomponentenabledsetting (componentname); if(res = =Packagemanager.component_enabled_state_default|| res = =packagemanager.component_enabled_state_enabled) {            //Hide App iconpackagemanager.setcomponentenabledsetting (componentname, packagemanager.component_enabled_state_disabled,        Packagemanager.dont_kill_app); } Else {            //Show app iconpackagemanager.setcomponentenabledsetting (componentname, Packagemanager.component_enabled_state_default,        Packagemanager.dont_kill_app); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.