5

默认情况下,当从应用商店下载应用时,iOS 会在快捷方式选项中提供“共享应用名称”选项。

参考下图:

在此处输入图像描述

单击它时,它会在手机菜单屏幕本身中打开共享意图,而无需重定向到应用程序,用户可以在其中共享应用程序。

在此处输入图像描述

我想在 android 中实现这个

这是我到目前为止所尝试的:

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

但这不起作用,因为我无法理解,这里的 targetClass应该是什么。

编辑: Nilesh 的回答几乎奏效了,但我现在面临的唯一问题是,每当我从快捷方式单击共享按钮时,启动器活动会显示一秒钟,然后显示共享意图选择器向上。现在当我按下显示共享选项的主页按钮时,应用程序将进入后台。这不应该发生。知道如何避免这种情况。

在此处输入图像描述

4

2 回答 2

3

您可以使用App shortcuts

但这个选项的主要缺点是它可以从 Oreo 和更高版本的 android 获得

这是示例代码

创建资源文件:res/xml目录(截图

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

现在您需要在清单文件中的活动标签下注册您的快捷方式

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

现在创建一个简单的分享活动来分享您的应用链接

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();
    }
}

输出

当用户长按应用程序图标时,您的快捷方式将如下图所示

在此处输入图像描述

点击快捷方式后

在此处输入图像描述

更新android:excludeFromRecents="true"在你的使用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>
于 2019-02-28T09:19:49.460 回答
0

有两种创建快捷方式的方法。

一:创建静态快捷方式,在应用的清单文件(AndroidManifest.xml)中,找到一个意图过滤器设置为android.intent.action.MAIN action和android.intent.category.LAUNCHER类别的activity。然后向此活动添加一个元素,该元素引用定义了应用程序快捷方式的资源文件:

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

...

:创建动态快捷方式你可以使用代码来做,ShortcutManager API 允许你对动态快捷方式完成以下操作:

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));

欲了解更多信息,您可以通过官方网站如何在android中创建快捷方式?

于 2019-02-28T08:36:15.700 回答