6

By default iOS is providing "Share appname" option in the shortcut options,when the app is downloaded from the app store.

Refer the image below:

enter image description here

When clicked on it,it is opening the share intent in the phone menu screen itself,without redirecting to the app,where the user can share the app.

enter image description here

I want to implement this in android.

Here is what I have I tried so far:

<shortcut
        android:shortcutId="share_app"
        android:enabled="true"
        android:icon="@drawable/ic_cart_active"
        android:shortcutShortLabel="@string/shortcut_share"
        android:shortcutLongLabel="@string/shortcut_share">
        <intent
            android:action="android.intent.action.send"
            android:targetPackage="com.example.android.internal"
            android:targetClass="" />
    </shortcut>

But this is not working,since I am not able to understand, what should be the targetClass over here.

EDIT: Nilesh`s answer almost worked,but the only problem that I am facing right now is,whenever I am clicking the share button from shortcut, the launcher activity is shown for a whisker of a second ,then the sharing intent chooser is showing up.Now when I am pressing the home button with the sharing options showing,the app is going to background.This should not happen.Any idea how to avoid this.

enter image description here

kgandroid
  • 5,507
  • 5
  • 39
  • 69

2 Answers2

3

You can use App shortcuts

but the main disadavtange of this option is that it is available from Oreo and above version of android

Here is the sample code

Create a resource file: res/xml directory (Screenshot)

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_share"
        android:shortcutDisabledMessage="@string/share_app"
        android:shortcutId="nilu"
        android:shortcutLongLabel="@string/share_app"
        android:shortcutShortLabel="@string/share_app">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="neel.com.demo.ShareActivity"
            android:targetPackage="neel.com.demo" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

Now you need to register your shortcut in your manifest file under activity tag

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="neel.com.demo">

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity" />

        <activity android:name=".ShareActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />

        </activity>

    </application>

</manifest>

Now create a simple share acivity to share your app link

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class ShareActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT,
                "Hey check out my app at: https://play.google.com/store/apps/details?id=neel.com.demo");
        sendIntent.setType("text/plain");
        startActivity(sendIntent);
        finish();
    }
}

OUTPUT

When user long press on app icon your shortcut will display like below image

enter image description here

after click of shortcut

enter image description here

UPDATE use android:excludeFromRecents="true" in your ShareActivity

<application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity" />

    <activity
        android:name=".ShareActivity"
        android:excludeFromRecents="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />

    </activity>

</application>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Let me implement this and will let you know.BDW this is what I want – kgandroid Feb 28 '19 at 09:21
  • @kgandroid happy to hear you that let me know if any help required – AskNilesh Feb 28 '19 at 09:22
  • Do I really need to give setContentView(R.layout.activity_home); in the Activity? – kgandroid Feb 28 '19 at 09:27
  • I have already another activity defined as launcher in Manifest.xml – kgandroid Feb 28 '19 at 09:30
  • @kgandroid no need to define share activity as defined as launcher – AskNilesh Feb 28 '19 at 09:59
  • One more thing,If I click on the home button with sharing intent open,then the app is appearing in the background.Any way to avoid that.In ios this is not happening – kgandroid Feb 28 '19 at 10:25
  • @NileshRathod looks good; just wouldn't show the uninstall button on the home-screen; that's bad for user retention and/or accidental clicks & pocket dialing... and shortcuts need an API level check, for the backwards compatibility. – Martin Zeitler Feb 28 '19 at 11:13
  • @MartinZeitler `just wouldn't show the uninstall button on the home-screen` that's system added sort cut on app icon so we can't control it and for `shortcuts need an API level check, for the backwards compatibility` it will work on android oreo version and above i have tested in Oreo and its working fine and also no other issue in marshmallow – AskNilesh Feb 28 '19 at 11:19
  • @NileshRathod that might be the dynamic shortcuts then. might have confused that. however, those dynamic shortcut menus do not have the uninstall button. that's a huge difference, besides how they are being generated (which ordinary is something the users doesn't care much - unless one permits their configuration). the static `XML` shortcuts still appear to be the direct equivalent to the Apple shortcuts. – Martin Zeitler Feb 28 '19 at 11:29
  • The only problem that I am facing right now is,whenever I am clicking the share button the launcher activity is shown for a whisker of a second ,then the sharing intent is showing up.Now when I am pressing the home button in that state,the app is going to background.This should not happen.Any idea how to avoid this? – kgandroid Feb 28 '19 at 12:34
  • 1
    Hi Nilesh,Thanks for your effort and time.Additionally I have added some extra flags to the ShareActivity to make it work as expected: sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sendIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); sendIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); – kgandroid Mar 06 '19 at 05:40
  • 1
    Also in the manifest: – kgandroid Mar 06 '19 at 05:40
  • @kgandroid happy to hear that – AskNilesh Mar 06 '19 at 05:43
0

there is two way to create shortcut.

one: create static shortcuts,In your app's manifest file (AndroidManifest.xml), find an activity whose intent filters are set to the android.intent.action.MAIN action and the android.intent.category.LAUNCHER category. and then add a element to this activity that references the resource file where the app's shortcuts are defined:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myapplication">
<application ... >
<activity android:name="Main">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>

  <meta-data android:name="android.app.shortcuts"
             android:resource="@xml/shortcuts" /> 
</activity>

...

two:Create dynamic shortcuts you can use code to do it,The ShortcutManager API allows you to complete the following operations on dynamic shortcuts:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(context, "id1")
.setShortLabel("Website")
.setLongLabel("Open the website")
.setIcon(Icon.createWithResource(context, R.drawable.icon_website))
.setIntent(new Intent(Intent.ACTION_VIEW,
               Uri.parse("https://www.mysite.example.com/")))
.build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

for more information, you can through the official website how to create shortcut in android?

Longalei
  • 453
  • 3
  • 8
  • I know how to create a shortcut.I need to create a shortcut that shares my app without launching it,just like ios – kgandroid Feb 28 '19 at 08:37