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); }