将用户从浏览器重定向到我的应用程序,在打开特定的URL后。

21

我写了一个应用程序,用户点击购买按钮后会重定向到互联网浏览器(例如:Chrome),付款后,我希望他返回我的应用程序(我的活动)。因此,我发现我应该使用Intent-Filter,但它对我不起作用!

我在清单中添加了这些代码:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:host="returnApp" android:scheme="myapp"></data>
</intent-filter>

当我打开像这样的链接:

myapp://returnApp?status=1

我的应用程序没有打开。

2个回答

18
尝���像这样打开它:myapp://returnApp/?status=1(添加尾部斜杠字符)。 这是因为路径参数被定义为默认值/
不幸的是,您无法完全匹配空字符串。正如文档所述:

URI的路径部分必须以“/”开头。

如果您确实需要使用确切的URLmyapp://returnApp?status=1来启动应用程序,则可以向数据子句添加android:pathPattern=".*"参数。
<intent-filter>
    ...
    <data android:host="returnApp" android:scheme="myapp" android:pathPattern=".*"></data>
</intent-filter>

android:pathPattern=".*"的Intent过滤器将匹配包括空路径在内的任何路径。


1
@esmaeil,你尝试从命令行启动应用程序了吗?当你在命令行shell中执行以下命令时,logcat中打印了什么内容:adb shell am start -a android.intent.action.VIEW -d 'myapp://returnApp?status=1' --debug-log-resolution - pepyakin
你好 @pepyakin,当我从 shell 中打开它时,它可以正常工作,但是当我从浏览器的网页中打开它时,它就无法工作了。你能告诉我问题出在哪里吗? - rana_sadam
@rana_sadam,你能给我展示一下你的意图过滤器、Shell命令和链接吗?这些是你用来从网页打开应用程序的吗? - pepyakin
我也在寻找同样的东西!代码请。 - deva11
如果您已经解决了这个问题,请分享代码。提前致谢。 - SNMohanty
android:pathPattern=".*" 运行得非常顺利。谢谢。 - Shahanshah Alam

6
  1. 打开Firebase应用程序
  2. 点击动态链接
  3. 在Firebase上创建自己的重定向回调链接,例如:https://example.page.link/payment

Check firebase image setup

  1. 像下面的代码一样在manifest.xml文件中设置链接

<intent-filter android:label="@string/app_name" android:autoVerify="true">
           <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
               <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https"
                    android:host="example.page.link" android:path="/payment"/>
                <data android:scheme="http"
                    android:host="example.page.link" android:path="/payment"/>
    </intent-filter>

  1. 运行你的应用程序
  2. 在安卓手机浏览器上检查 Firebase 链接
  3. 它能正常工作了,是的

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接