How to know if application is disabled in Android ICS

前端 未结 3 1323
温柔的废话
温柔的废话 2020-12-18 03:51

In Android ICS we have an ability to disable built-in applications. Is it possible to know if specific app is disabled or enabled in code?

相关标签:
3条回答
  • 2020-12-18 04:25

    Try http://developer.android.com/reference/android/content/pm/PackageManager.html#getApplicationInfo%28java.lang.String,%20int%29 and check the 'enabled' flag in the returned ApplicationInfo object.

    0 讨论(0)
  • 2020-12-18 04:30

    The Accepted answer may lead to an exception as well, i.e, NameNotFoundException and therefore you may have to construct a flow that silently catches the exception and decide the enabled state(it would actually be a third state, i.e, not-installed).

    So, it would be better to find the enabled as well as installed state like this:

    public static final int ENABLED = 0x00;
        public static final int DISABLED = 0x01;
        public static final int NOT_INSTALLED = 0x03;
    
        /**
         * @param context Context
         * @param packageName The Package name of the concerned App
         * @return State of the Application.
         *
         */
        public static int getAppState(@NonNull Context context, @NonNull String packageName) {
            final PackageManager packageManager = context.getPackageManager();
    
            // Check if the App is installed or not first
            Intent intent = packageManager.getLaunchIntentForPackage(packageName);
            if (intent == null) {
                return NOT_INSTALLED;
            }
            List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            if(list.isEmpty()){
                // App is not installed
                return NOT_INSTALLED;
            }
            else{
                // Check if the App is enabled/disabled
                int appEnabledSetting = packageManager.getApplicationEnabledSetting(packageName);
                if(appEnabledSetting == COMPONENT_ENABLED_STATE_DISABLED || 
                        appEnabledSetting == COMPONENT_ENABLED_STATE_DISABLED_USER){
                    return DISABLED;
                }
                else{
                    return ENABLED;
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-18 04:31
    ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo("your_package",0);
    
    boolean appStatus = ai.enabled;
    

    if appStatus is false then app is disabled :)

    0 讨论(0)
提交回复
热议问题