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_开发者_StackOverflow社区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 :D
The Android SDK docs explain both: onDisabled
and onEnabled
.
In a nutshell, onEnabled
is called when the first instance of the widget is created, and onDisabled
is called when the last instance of the widget is deleted/removed.
Edit: in reference to the setComponentEnabledSetting
calls, I believe they simply used to indicate that no widgets are active and therefore any related backend processing can be halted.
精彩评论