如果app没有安装在手机中,如何禁用按钮

时间:2017-12-04 12:21:36

标签: java android

我有按钮,我希望禁用此按钮,如果未安装包的应用程序,用户可以点击,如果应用程序安装在手机..

我的代码

    Button start = (Button) findViewById(R.id.bt_start);
    boolean isAppInstalled = appInstalledOrNot("com.check.application");
    if(isAppInstalled) {
        Intent Start_screen = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(Start_screen);

    } else {

        start.setEnabled(false);

    }
}

private boolean appInstalledOrNot(String uri) {
    PackageManager pm = getPackageManager();
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
    }

    return false;
}

我使用此代码,但该按钮始终处于禁用状态..如果应用安装按钮禁用,如果没有安装按钮禁用

2 个答案:

答案 0 :(得分:0)

为什么不尝试以下方法来检查android中是否存在包

public boolean isPackageExisted(String targetPackage){
    List<ApplicationInfo> packages;
    PackageManager pm;

    pm = getPackageManager();        
    packages = pm.getInstalledApplications(0);
    for (ApplicationInfo packageInfo : packages) {
        if(packageInfo.packageName.equals(targetPackage))
            return true;
    }
    return false;
}

使用targetPacket调用方法isPackageExisted。

答案 1 :(得分:0)

>>>import unicodedata
>>>a = 'A Coruña'
>>>search_string = ''.join((c for c in unicodedata.normalize('NFD', a)if unicodedata.category(c) != 'Mn'))
>>>print(search_string)
>>>A Coruna