我现在执行的一个程序,尽管用户可以读取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 reader ?
String dirPath = "/data/data/" + getPackageName() + "/files/test.apk"; //pdf包存储地址 Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
使用下面的代码试试:
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();
}
}