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
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
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);