【问题标题】:how to download adobe reader programatically if not exists如果不存在,如何以编程方式下载 adobe reader
【发布时间】:2012-03-17 19:30:37
【问题描述】:

现在我正在开发一个应用程序。通过我的应用程序,用户可以阅读 pdf 文件,如果没有 pdf 阅读器,那么我的应用程序会自动从网站安装它。 这是我用来读取pdf文件的代码。

File file = new File("/sdcard/sample.pdf");
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0 && file.isFile()) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");
    startActivity(intent);
}

我的疑惑是:

  1. 如何查看手机中是否安装了adobe reader?
  2. 如何以编程方式在手机上安装 adobe 阅读器?

【问题讨论】:

    标签: android


    【解决方案1】:

    你的代码有点复杂..

    使用此代码,

    Intent intent;
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(file, "application/pdf");
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                // No application to view, ask to download one
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("No Application Found");
                builder.setMessage("Download one from Android Market?");
                builder.setPositiveButton("Yes, Please",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                                marketIntent
                                        .setData(Uri
                                                .parse("market://details?id=com.adobe.reader"));
                                startActivity(marketIntent);
                            }
                        });
                builder.setNegativeButton("No, Thanks", null);
                builder.create().show();
            }
        }
    

    【讨论】:

    • 非常感谢你.....我在模拟器中测试了你的代码......那个警报对话框来了......然后我按下确定然后它给出了异常......你认为......它是因为我在模拟器中测试..它可以在手机中使用吗??
    • @sarath 模拟器不附带市场应用程序,请参阅stackoverflow.com/questions/3994923/…
    • 你用这段代码编程下载过Adobe reader吗?
    • 当我在 pc 模拟器中工作时,如果安装了 pdf 阅读器,它会打开文件,否则应用程序会显示一个带有选项的警报框。但是当我在手机应用程序中运行它时,会显示文件无法打开。我的手机没有 adobe reader,所以它必须显示一个 alertdialog。但它只是显示文件无法打开没有任何 alertdialogbox
    • 这是因为您的手机有任何 pdf 阅读器,无法打开该文件。确保条件仅用于检查 Intent 类型“application/pdf”而不是用于应用程序 adobe 阅读器,如果您的设备上有任何可用的应用程序处理此 Intent 类型,则不会出现对话框。
    【解决方案2】:

    我认为这可能会对您有所帮助:

    private void loadDocInReader(String doc) throws ActivityNotFoundException, Exception {
    
    try {
        Intent intent = new Intent();
    
        intent.setPackage("com.adobe.reader");
        intent.setDataAndType(Uri.parse(doc), "application/pdf");
    
        startActivity(intent);
    
    } 
    
    catch (ActivityNotFoundException activityNotFoundException) {
                activityNotFoundException.printStackTrace();
    
                throw activityNotFoundException;
    } 
    catch (Exception otherException) {
                otherException.printStackTrace();
    
                throw otherException;
    }
    }
    

    如果未安装 Adob​​e 阅读器,您可以将用户拖到此网址:

    https://market.android.com/details?id=com.adobe.reader

    这将在 Android Market 移动应用程序中打开 Adob​​e Reader。如果用户愿意,他们可以安装。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 2019-09-07
      相关资源
      最近更新 更多