如何知道Android ICS中是否禁用了应用程序

时间:2012-07-18 09:36:36

标签: android

在Android ICS中,我们可以禁用内置应用程序。 是否可以知道代码中是否禁用或启用了特定应用程序?

3 个答案:

答案 0 :(得分:8)

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

boolean appStatus = ai.enabled;

如果appStatusfalse,则应用已停用:)

答案 1 :(得分:1)

接受的答案也可能导致异常,即NameNotFoundException,因此您可能必须构建一个静默捕获异常并确定启用状态的流(它会实际上是第三个状态,即未安装。

因此,最好找到启用状态和已安装状态,如下所示:

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;
            }
        }
    }

答案 2 :(得分:0)