Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if application is disabled in Android ICS

Tags:

android

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?

like image 608
a.toropov Avatar asked Jul 18 '12 09:07

a.toropov


2 Answers

ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo("your_package",0);
                    
boolean appStatus = ai.enabled;

if appStatus is false then app is disabled :)

Edit:

On Android 11 (API level 30) you also need to tell the manifest which apps you want to gather information from:

<manifest package="com.example.game">
    <queries>
        <package android:name="com.example.store" />
    </queries>
</manifest>

Source: https://developer.android.com/training/basics/intents/package-visibility#package-name

like image 152
Amir Avatar answered Sep 18 '22 13:09

Amir


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;
            }
        }
    }
like image 44
Manish Kumar Sharma Avatar answered Sep 18 '22 13:09

Manish Kumar Sharma