【问题标题】:Share intent option using Shortcut android使用 Shortcut android 共享意图选项
【发布时间】:2019-02-26 08:32:14
【问题描述】:

默认情况下,当从应用商店下载应用时,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 的回答几乎奏效了,但我现在面临的唯一问题是,每当我从快捷方式单击共享按钮时,启动器活动都会显示为一秒钟,然后显示共享意图选择器。现在,当我按下显示共享选项的主页按钮时,应用程序将进入后台。这不应该发生。知道如何避免这种情况。

【问题讨论】:

    标签: android android-shortcut


    【解决方案1】:

    您可以使用App shortcuts

    但这个选项的主要缺点是它可以从 Oreo 及以上版本的 android 中获得

    这里是示例代码

    创建资源文件:res/xml目录(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>
    

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

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

    输出

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

    点击快捷方式后

    UPDATE 在您的 ShareActivity

    中使用 android:excludeFromRecents="true"
    <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>
    

    【讨论】:

    • 让我来实现它并让你知道。BDW 这就是我想要的
    • @kgandroid 很高兴听到您告诉我是否需要任何帮助
    • 我真的需要给 setContentView(R.layout.activity_home);在活动中?
    • 嗨 Nilesh,感谢您的努力和时间。此外,我在 ShareActivity 中添加了一些额外的标志以使其按预期工作:sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sendIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); sendIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    • 也在清单中:
    【解决方案2】:

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

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

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

    更多信息可以通过官网how to create shortcut in android?

    【讨论】:

    • 我知道如何创建快捷方式。我需要创建一个快捷方式来共享我的应用程序而不启动它,就像 ios 一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多