Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android create shortcuts on the home screen

What I want to do is:

1) I'm inside an activity, there are 2 buttons. If I click the first one a shortcut is created in my home screen. The shortcut open an html page that has been previously downloaded, so I want it to use the default browser but I don't want to use internet cause I already have the page.

2)The second button create another shortcut that starts an activity. And i want to pass to the activity some extra arguments (As strings for example)...........

Are those things possible? I found some link and some similar questions like Android: Is there a programming way to create a web shortcut on home screen

They seem to be the answer to my question but someone told me that this code is not gonna work on all devices and that is deprecated and that what i want to do is not possible.......

This technique is not recommended. This is an internal implementation, not part of the Android SDK. It will not work on all home screen implementations. It may not work on all past versions of Android. It may not work in future versions of Android, as Google is not obligated to maintain internal undocumented interfaces. Please do not use this

What means internal implementation? Is that code trustable or not.....help me pls.....

like image 955
Sgotenks Avatar asked Jun 13 '11 23:06

Sgotenks


People also ask

How do I create a custom shortcut on Android?

For rearranging or adding new shortcuts just tap and hold on one of the items on your rotating wheel. For further customization, you can also access the Omni Swipe app from your app drawer.

How do I add a shortcut to my Samsung Home screen?

To add shortcut to Home screen, tap and hold an app and select Add shortcut to Home option. Alternatively, drag the icon to the top to add shortcut on Home screen. To delete shortcut on Home screen, tap and hold an app on Home and select Remove shortcut option.


2 Answers

The example code uses undocumented interfaces (permission and intent) to install a shortcut. As "someone" told you, this may not work on all phones, and may break in future Android releases. Don't do it.

The correct way is to listen for a shortcut request from the home screen-- with an intent filter like so in your manifest:

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">   <intent-filter>     <action android:name="android.intent.action.CREATE_SHORTCUT" />     <category android:name="android.intent.category.DEFAULT" />   </intent-filter> </activity> 

Then in the activity that receives the intent, you create an intent for your shortcut and return it as the activity result.

// create shortcut if requested ShortcutIconResource icon =     Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);  Intent intent = new Intent();  Intent launchIntent = new Intent(this,ActivityToLaunch.class);  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname()); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);  setResult(RESULT_OK, intent); 
like image 178
antlersoft Avatar answered Sep 19 '22 17:09

antlersoft


I have developed one method below for creating the shortcut icon on android Homescreen [Tested on my own app]. Just call it.

private void ShortcutIcon(){      Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);     shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);      Intent addIntent = new Intent();     addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);     addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");     addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));     addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");     getApplicationContext().sendBroadcast(addIntent); } 

Don't forget to change your activity name, icon resource & permission.

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 

Happy coding !!!

Edit:

For Duplicate Issue, First Option is to add below line in the code otherwise it creates new one every time.

addIntent.putExtra("duplicate", false); 

Second Option is to First uninstall The App Shortcut Icon and then Install It Again If the First Option Didn't Work.

like image 30
Siddiq Abu Bakkar Avatar answered Sep 17 '22 17:09

Siddiq Abu Bakkar