当前位置:主页 > 移动开发 > Android代码 >

Android 删除指定包名的App实例代码

时间:2021-01-06 11:21:40 | 栏目:Android代码 | 点击:

废话不多说了,直接给大家贴代码了,具体代码如下所示;

/**
   * check and delete the old package app if it exists.
   */
  private void checkOldPackage() {
    String packageName = "xxx.xxx.xxx.xxx";
    if (isAvilible(this, packageName)) {
      Intent uninstall_intent = new Intent();
      uninstall_intent.setAction(Intent.ACTION_DELETE);
      uninstall_intent.setData(Uri.parse("package:" + packageName));
      startActivity(uninstall_intent);
    }
  }
  private boolean isAvilible(Context cxt, String packagename) {
    PackageManager pm = cxt.getPackageManager();
    List<PackageInfo> pinfo = pm.getInstalledPackages(0);
    for (int i = 0; i < pinfo.size(); i++) {
      if (pinfo.get(i).packageName.equalsIgnoreCase(packagename)) {
        return true;
      }
    }
    return false;
  }

1.查找是否存在指定包名的App

2.创建Intent,删除指定包名的App

总结

您可能感兴趣的文章:

相关文章