0

I have button and i want disable this button if app by Package not installed and user can click if app install in phone ..

my code

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

I use this code but the button is disabled at all times .. if app install the button disable and if not install the button disable

ah3ds
  • 13
  • 1

2 Answers2

0

Why don't you try below method to check for existence of package in 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;
}

Call method isPackageExisted with targetPacket.

dpaksoni
  • 327
  • 3
  • 17
0
     boolean isAppInstalled = appInstalledOrNot("com.check.application");  
    btn.setEnabled(isAppInstalled) 

        private boolean appInstalledOrNot(String uri) {
            PackageManager pm = getPackageManager();
            try {
                pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
                return true;
            } catch (PackageManager.NameNotFoundException e) {
                  return false;
            }
Nirav Joshi
  • 1,713
  • 15
  • 28