Skip to content

Instantly share code, notes, and snippets.

@sidhuparas
Last active January 23, 2018 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidhuparas/9c2bf6f54abf41ca0a724939844ec318 to your computer and use it in GitHub Desktop.
Save sidhuparas/9c2bf6f54abf41ca0a724939844ec318 to your computer and use it in GitHub Desktop.
//Pre-Oreo
private void addShortcut(String path1, String pdfName) {
File file = new File(path1);
if (file.exists()) {
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, pdfName);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this.getApplicationContext(),image));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
} else {
Toast.makeText(this, "Some error occurred!", Toast.LENGTH_SHORT).show();
}
}
//Oreo and above
private void addShortcutInOreo(String path1, String pdfName){
try {
File file = new File(path1);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
if (Build.VERSION.SDK_INT > 25) {
ShortcutManager shortcutManager;
shortcutManager = getSystemService(ShortcutManager.class);
if (blue.isChecked())
image = R.drawable.pdf;
else image = R.drawable.pdf2;
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, pdfName)
.setShortLabel(pdfName)
.setLongLabel(pdfName)
.setIcon(Icon.createWithResource(this, image))
.setIntent(pdfIntent)
.build();
shortcutManager.requestPinShortcut(shortcut, null);
}
}catch (Exception e){
Toast.makeText(this, "Some error occurred: "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment