开发者

onEnabled/onDisabled methods with "setComponentEnabledSettings" understanding

开发者 https://www.devze.com 2023-02-03 04:24 出处:网络
I was studying a piece of source code from the original PowerControl Widget (SettingsAppWidgetProvider) and I\'ve found the following methods:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消