原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/491
问题描述:
我的一个应用程序是这样实现:通过我的应用程序,用户可以阅读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阅读器?
解决方案:
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();
}
}