Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create shortcut for specific activity on home screen

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.

like image 863
Juvi Avatar asked Jun 14 '15 13:06

Juvi


People also ask

How create programmatically shortcut Android home screen?

INSTALL_SHORTCUT which can be used to add shortcuts to home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut. First we need to add permission INSTALL_SHORTCUT to android manifest xml. The addShortcut() method creates a new shortcut on Home screen.

How do I create a shortcut on my Samsung phone?

Add shortcuts to Home screens Swipe up or down to access the Apps screen. There are multiple ways to add shortcuts to the Home screen: Touch and hold the app until a menu displays, and then tap Add to Home. Touch and hold the app until the Home screen is displayed, and then release the app in your desired location.


1 Answers

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.

like image 153
Himanshu arora Avatar answered Sep 25 '22 14:09

Himanshu arora