如何知道设备上是否安装了特定应用程序?

时间:2017-05-31 05:16:13

标签: android

我想检查设备上是否安装了应用。我正在使用以下代码:

total_sales = 0

CSV.foreach('sales-data.csv', headers: true, converters: :all) do |row|

  if row[2] == "Music"

    # TODO: check if category is "Music" (row[2])

    # TODO: if it is music, add total_sales + the row's sales (row[6])

    total_sales = total_sales + row[6]

  end

   end

puts total_sales.round(2)

这给了我所有的包裹但不包括我的那些包裹。 我试图找到这个应用程序:rootcloak和暴露的安装程序 无法使用上述代码找到这些应用。

4 个答案:

答案 0 :(得分:4)

使用以下代码:

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

调用方法如:

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

isAppInstalled("com.randomname", "anyname"); //it will return false

答案 1 :(得分:1)

像phatsapp一样

shareImage(“com.whatsapp”,“Whatsapp”);

呼叫

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

答案 2 :(得分:0)

只需使用特定应用的应用ID进行检查 例如 检查

getPackageManager()。getPackageInfo(“com.facebook.katana”,0);

return true;

否则   假

查看此答案可能对您有所帮助。 How to check programmatically if an application is installed or not in Android?

答案 3 :(得分:0)

我找到了答案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;

这对我很有帮助。