编程语言
首页 > 编程语言> > android-如何在单击应用程序链接时打开我的应用程序?

android-如何在单击应用程序链接时打开我的应用程序?

作者:互联网

我想要共享我的应用程序特定页面的链接.假设我在我的应用程序中某个用户的个人资料页面中,并且想要将该用户共享给我的一些朋友,所以我想通过WhatsApp和其他应用程序共享它.

所以基本上我想共享一个指向我的应用程序的链接.

这就是我现在正在做的

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + activity.getPackageName());
    activity.startActivity(Intent.createChooser(shareIntent, "Share"));

我知道这不是我真正想要的,但已部分完成.
如果用户尚未安装我的应用,则此代码会将其重定向到Google Play商店页面以下载此应用.

主要问题是,当用户已经拥有此应用程序时,此链接只会打开该应用程序.我想在用户单击链接时将用户重定向到该特定页面.

我怎样才能做到这一点?

我听说过有关Intent过滤器的信息,但不确定如何使用它们.

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/pdf" />

更新

现在,我试图像这样分享我的应用程序链接

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra("PostID", postID);
    shareIntent.putExtra("UserID", userID);
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + activity.getPackageName());
    activity.startActivity(Intent.createChooser(shareIntent, "Share"));

我在清单中声明了这样的活动

<activity
        android:name=".activities.ShareActivity"
        android:screenOrientation="sensorPortrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">

        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="details"
                android:scheme="market" />
            <data
                android:host="play.google.com"
                android:pathPattern="/store/apps/details?id=my_package_name"
                android:scheme="http" />
            <data
                android:host="play.google.com"
                android:pathPattern="/store/apps/details?id=my_package_name"
                android:scheme="https" />
        </intent-filter>
    </activity>

但是,当我单击使用WhatsApp或其他任何方式共享的链接时,仍然看不到我的应用程序.

请帮我

任何帮助将不胜感激.
提前致谢.

解决方法:

在您的项目的AndroidManifest中,将以下intent-filters添加到您的活动声明中:

    <intent-filter>

        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>

        <data android:mimeType="text/plain"/>

    </intent-filter>

现在,在活动生命周期的onCreate(),onResume()或onNewIntent()方法中,您可以使用触发了活动的意图的getData().

标签:android-intent,intentfilter,android
来源: https://codeday.me/bug/20191119/2032977.html