How to know if a particular application is install

2020-05-07 18:23发布

I want to check whether an app is installed on the device or not. I am using code below :

PackageManager pm = context.getPackageManager();
        List<PackageInfo> packageInfoList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);
        if (packageInfoList != null) {
            for (PackageInfo packageInfo : packageInfoList) {
                Log.d(TAG, "-->"+packageInfo.packageName);
                String packageName = packageInfo.packageName;
                if (packageName != null && packageName.equals(uri)) {
                    return true;
                }
            }
        }
        return false;

Which gives me all the packages but not the one that I except. I am trying to find this app : rootcloak and exposed installer Not able to find these apps with the code above.

标签: android
4条回答
ゆ 、 Hurt°
2楼-- · 2020-05-07 18:42

just check using application id of particular app for example check

getPackageManager().getPackageInfo("com.facebook.katana", 0);

return true;

else false

check this answer it may helpful to you. How to check programmatically if an application is installed or not in Android?

查看更多
神经病院院长
3楼-- · 2020-05-07 18:47

Use Below Code:

public boolean isAppInstalled(String package_name, String app_name)
{
    try {
        PackageManager pm = getPackageManager();
        PackageInfo info = pm.getPackageInfo("" + package_name, PackageManager.GET_META_DATA);
        return true;

    }
    catch (PackageManager.NameNotFoundException e) {
        Toast.makeText(getApplicationContext(), "Your device has not installed " + app_name, Toast.LENGTH_SHORT)
                .show();
        return false;
    }
}

Call the method like:

isAppInstalled("com.whatsapp", "Whatsapp"); // it will return true if your device is having whatsApp.

isAppInstalled("com.randomname", "anyname"); //it will return false
查看更多
我想做一个坏孩纸
4楼-- · 2020-05-07 18:53

I found the answer here

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = 
context.getPackageManager().queryIntentActivities( mainIntent, 0);
for (ResolveInfo resolveInfo : pkgAppsList) {
            Log.d(TAG, "__<>"+resolveInfo.activityInfo.packageName);
            if (resolveInfo.activityInfo.packageName != null 
                    && resolveInfo.activityInfo.packageName.equals(uri)) {
              return true;
          }
        }
        return false;

This worked for me perfectly.

查看更多
姐就是有狂的资本
5楼-- · 2020-05-07 19:05

like whatsapp

shareImage("com.whatsapp", "Whatsapp");

call

public void shareImage(String pkgname, String appname) {
  String path = null;
  try {
    path = MediaStore.Images.Media.insertImage(getContentResolver(),
    arrImagePath.get(slidePager.getCurrentItem()), "Title", null);
  } catch (FileNotFoundException e1) {
    e1.printStackTrace();
  }
  Uri uri = Uri.parse(path);
  Intent share = new Intent(Intent.ACTION_SEND);
  share.setPackage(pkgname);
  share.putExtra(Intent.EXTRA_STREAM, uri);
  share.setType("image/*");
  share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  startActivity(Intent.createChooser(share, "Share image File");
}
查看更多
登录 后发表回答