Android 在主屏幕上创建快捷方式以打开PDF文件
在我的android应用程序中,我想让用户在主屏幕上创建一个快捷方式,打开位于特定位置的PDF文件。在我的研究中,我发现我们可以创造捷径来打开特定的活动。但在我的例子中,我需要使用任何已安装的PDF阅读器打开特定的PDF文件 我知道这与意图有关。谢谢你的帮助 编辑1:以下是我尝试的代码。它不会打开文件,而是打开应用程序。请告诉我怎么了Android 在主屏幕上创建快捷方式以打开PDF文件,android,Android,在我的android应用程序中,我想让用户在主屏幕上创建一个快捷方式,打开位于特定位置的PDF文件。在我的研究中,我发现我们可以创造捷径来打开特定的活动。但在我的例子中,我需要使用任何已安装的PDF阅读器打开特定的PDF文件 我知道这与意图有关。谢谢你的帮助 编辑1:以下是我尝试的代码。它不会打开文件,而是打开应用程序。请告诉我怎么了 File file = new File(Environment.getExternalStorageDirectory()+"/CDLU Mathematics
File file = new File(Environment.getExternalStorageDirectory()+"/CDLU Mathematics Hub","Time Table For Even Semesters (2017).pdf");
Uri path = Uri.fromFile(file);
Intent shortcutIntent = new Intent(getApplicationContext(), Home.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
shortcutIntent.setDataAndType(path, "application/pdf");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TimeTable");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.icon));
addIntent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
多亏了@commonware,我找到了答案。代码如下:
File file = new File(Environment.getExternalStorageDirectory()+"/Folder Name","File Name.pdf");
Uri path = Uri.fromFile(file);
Intent shortcutIntent = new Intent(Intent.ACTION_VIEW);
shortcutIntent.setDataAndType(path, "application/pdf");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TimeTable");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
好的,
操作\u创建\u快捷方式
不限于特定的活动。创建一个指向您的PDF文件的ACTION\u视图
Intent
,然后将该Intent
与ACTION\u Create\u快捷方式
一起使用。您能不能用示例代码将其作为答案发布,以便我可以将其标记为答案?请检查我的编辑替换新的意图(getApplicationContext(),Home.class)代码>具有新的意图(意图.行动\u视图)
并去掉单独的shortcutitent.setAction(Intent.ACTION\u MAIN)代码>行。