Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.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?

like image 267
Kartik Domadiya Avatar asked Jun 21 '11 11:06

Kartik Domadiya


1 Answers

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" />
like image 52
Niranj Patel Avatar answered Oct 14 '22 10:10

Niranj Patel