Reputation: 2221
I am trying to find out how to create URL shortcut in Android home screen programmatically and couldn't find the answer. It was asked once on StackOverflow, but the only answer had nothing to do with the question and has -2 ranking. Can someone please help?
Thanks in advance.
Upvotes: 0
Views: 579
Reputation: 1538
private void setShortcut(String url) {
String query = Uri.encode(url, "UTF-8");
browserIntent = new Intent(CATEGORY_BROWSABLE, Uri.parse(Uri.decode(query)));
browserIntent.setAction(ACTION_VIEW);
if (ShortcutManagerCompat.isRequestPinShortcutSupported(getApplicationContext())) {
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(getApplicationContext(), "1")
.setIntent(browserIntent) // !!! intent's action must be set on oreo
.setShortLabel("Test")
.setIcon(IconCompat.createWithResource(getApplicationContext(), R.drawable.bg))
.build();
ShortcutManagerCompat.requestPinShortcut(getApplicationContext(), shortcutInfo, null);
} else {
Toast.makeText(getApplicationContext(),"launcher does not support short cut icon",Toast.LENGTH_LONG).show();
}
}
I have tested this on emulator and it works and opens url in the Browser you can use .setIntent(browserIntent)
method to set intent to a shortcut and in the code I have created browserIntent which opens the url in a browser
Upvotes: 1