5

I would like to give my users an option to create a shortcut to specific page within the app. I've seen similar usage at Whatsapp when you long press a chat and you are able to create a desktop shortcut to this specific chat.

I've tried finding some documentation about this functionality but couldn't get it working. Here's what I have:

activity which isn't the launcher activity (including the intent-filter)

 <activity android:name="com.my.example.pages.Topics"
    android:parentActivityName="com.my.example.pages.Apps">
        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

createShortcut function

 public void createShortcut(){
        Intent shortcutIntent = new Intent("com.my.example.pages.Topics");
        Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(getActivity(), R.drawable.app_logo);

        // The result we are passing back from this activity
        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Test");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        getActivity().setResult(getActivity().RESULT_OK, intent);
        getActivity().finish();

        Toast.makeText(getActivity(),"Shortcut created",Toast.LENGTH_SHORT).show();
    }

Manifest

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

I'm probably missing something since after calling the function I get the Toasts but there's no shortcut created and the app exits because of the finish() method.

To be more clearer - how do I create shortcut for non-launcher activity?

*I'm running the code within one of my viewpager fragments.

Community
  • 1
  • 1
Juvi
  • 1,078
  • 1
  • 19
  • 38
  • Related: http://stackoverflow.com/questions/6988511/how-to-add-apps-shortcut-to-the-home-screen – cygery Jun 14 '15 at 14:23
  • With all the respect to the guy who answered, Whatsapp application doing it very good and smoothly. As a user I feel that this option is very useful and even necessary – Juvi Jun 14 '15 at 15:05
  • Seems for this to work, "Topics" must be a launcher activity – Nana Ghartey Jun 14 '15 at 15:26
  • Did you try the exact steps shown in the linked question? – cygery Jun 14 '15 at 15:27
  • @NanaGhartey why it has to be the launcher activity? You can see that Whatsapp chat's page isn't the launcher activity and the shortcut leads directly to the chat – Juvi Jun 14 '15 at 17:10
  • @cygery The related question is targetting for shortcut to the launcher activity. Google play already doing it for you – Juvi Jun 14 '15 at 17:34
  • Then what exactly *different* do you want? – cygery Jun 14 '15 at 21:25
  • Shortcut to a non-launcher activity – Juvi Jun 15 '15 at 07:17
  • Actually, Whatsapp start the main activity and from it it opens the person's chat. That's the reason when you press button it has the main home screen ready without loading it. – kirtan403 Jan 06 '16 at 18:47

2 Answers2

7

Use this to create a shortcut for non-launcher activity.

    private void createShortcutOfApp() {

        Intent shortcutIntent = new Intent(getApplicationContext(),
            YourTargetActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "App shortcut name");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext(getApplicationContext(),
        R.mipmap.logo_of_your_app_shortcut));

        addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        addIntent.putExtra("duplicate", false);  //may it's already there so   don't duplicate
        getApplicationContext().sendBroadcast(addIntent);
    }

Now add permission in manifest

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

Now define

android:exported="true"

attribute in

<activity> tag 

like

<activity
  android:name=".YourTargetActivity"
  android:exported="true"></activity>

This works like whatsapp app chat shortcut.

Himanshu arora
  • 161
  • 4
  • 11
0

After referring to the link(pinned shortcuts), the following kotlin code worked for me

fun createWebActivityShortcut(context:Context,shortcutname:String,extra1:String) {

    val shortcutManager = ContextCompat.getSystemService<ShortcutManager>(
        context,
        ShortcutManager::class.java
    )

    if (shortcutManager!!.isRequestPinShortcutSupported) {
        val pinShortcutInfoBuilder = ShortcutInfo.Builder(context, "yourapp_"+shortcutname)
        pinShortcutInfoBuilder.setShortLabel(shortcutname)
        val intent = Intent(Intent.ACTION_VIEW, null, context, YourSpecificActivity::class.java)
        intent.putExtra("extra1",extra1) //add as many extras as you like
        pinShortcutInfoBuilder.setIntent(intent)
        pinShortcutInfoBuilder.setIcon()//add your icon here
        val pinShortcutInfo = pinShortcutInfoBuilder.build()

        val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(
            pinShortcutInfo
        )

        val successCallback = PendingIntent.getBroadcast(
            context, /* request code */ 0,
            pinnedShortcutCallbackIntent, /* flags */ 0
        )

        shortcutManager.requestPinShortcut(
            pinShortcutInfo,
            successCallback.intentSender
        )
    }
}
an0nym0use
  • 311
  • 1
  • 4
  • 8