I was studying a piece of source code from the original PowerControl Widget (SettingsAppWidgetProvider) and I’ve found the following methods:
@Override
public void onEnabled(Context context) {
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(new ComponentName("com.android.settings",
".widget.SettingsAppWidgetProvider"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
@Override
public void onDisabled(Context context) {
Class clazz = com.android.settings.widget.SettingsAppWidgetProvider.class;
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(new ComponentName("com.android.settings",
".widget.SettingsAppWidgetProvider"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
Can someone explain me what they do exactly??
edit: I’m sorry my question was bad formulated..I know when they are called but I can’t understand what the setComponentEnabledSetting pair do 😀
The Android SDK docs explain both:
onDisabledandonEnabled.In a nutshell,
onEnabledis called when the first instance of the widget is created, andonDisabledis called when the last instance of the widget is deleted/removed.Edit: in reference to the
setComponentEnabledSettingcalls, I believe they simply used to indicate that no widgets are active and therefore any related backend processing can be halted.