-1

I want to check if multiple application is installed or not in user device and always get toast Application is not currently installed but whatsapp and instagram is installed. why?

  String [] strings = new String [] {"com.whatsapp", "com.android.instagram" };
        List<String> stringList = new ArrayList<String>(Arrays.asList(strings)); 



        boolean isAppInstalled = appInstalledOrNot(String.valueOf(stringList));
        if(isAppInstalled) {

            Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(String.valueOf(stringList));startActivity(LaunchIntent);
            Toast.makeText(MainActivity.this,"Application is already installed.",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this,"Application is not currently installed.",Toast.LENGTH_SHORT).show();
        }
    }

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

        return false;
    }
J.was2a
  • 27
  • 2

2 Answers2

0

You are passing wrong parameter. Try something like below.

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

    return false;
}

public void check(){
String [] strings = new String [] {"com.whatsapp", "com.android.instagram" };
for(String appPackageName: strings) {
    boolean isAppInstalled = appInstalledOrNot(appPackageName);
    if(isAppInstalled) {
        Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(appPackageName);
        startActivity(LaunchIntent);
        Toast.makeText(MainActivity.this,appPackageName+" is already installed.",Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(MainActivity.this,appPackageName+" is not currently installed.",Toast.LENGTH_SHORT).show();
    }
}
}

Note:- Do not fire LaunchIntentinside loop . first check which applications are installed and then you can select from list(As per your need cause i have no knowledge of it).

Or you can directly check for each one separately:

 boolean isWhatsAppinstalled = appInstalledOrNot("com.whatsapp");
 boolean isInstainstalled = appInstalledOrNot("com.android.instagram");
ADM
  • 20,406
  • 11
  • 52
  • 83
  • this code give all toast "is already installed." and "is not currently installed" than close app :( – J.was2a Mar 28 '18 at 08:53
  • Yeah i will give all . I just pointed out your mistake . I am not aware of what exactly your need is . – ADM Mar 28 '18 at 08:54
0

Since you copy pasted this code from https://stackoverflow.com/a/36419540/4467208 you should actually also modify it to your needs. You can not pass your String concatenation to appInstalledOrNot since it does not support it whatsoever.

Instead change it to

boolean isAppInstalled = appInstalledOrNot("com.whatsapp") && appInstalledOrNot("com.android.instagram");

If you want to show different Toasts for different cases then you have to call appInstalledOrNot one by one.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107