Creating shortcuts in Android via Intent [duplicate]
Possible Duplicate:
Android create shortcuts on the home screen
I am having one TextView
and Button
in my Activity (HomeActivity.class).
Goal: When I click on the Button
it should create a shortcut with the default android image as icon and text as entered in the TextView
.
So far I've found out that we can create shortcuts using ACTION_CREATE_SHORTCUT
This is the code I have so far:
Intent result = new Intent(android.content.Intent.ACTION_CREATE_SHORTCUT);
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(getApplicationContext(), Editor.class));
result.putExtra(TodoDbAdapter.KEY_ROWID,rowid);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME,textView.getText());
result.putExtra(Intent.开发者_StackOverflow社区EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(HomeActivity.this,
R.drawable.icon));
setResult(RESULT_OK, result);
My Manifest File :
<activity android:name=".HomeActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
But this code does not create any shortcut.
How can I create shortcuts?
Try this:
Intent shortcutIntent;
shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(activity.getPackageName(), ".classname"));
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent putShortCutIntent = new Intent();
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Title");
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(PersonProfile.this, R.drawable.icon));
putShortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(putShortCutIntent);
and add this permission in manifest:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
精彩评论