android - 如何重定向回 Android 应用程序?

标签 android redirect webview google-oauth

我需要打开一个自定义 Chrome 选项卡,将用户带到我的网站,然后将用户重定向回应用。它类似于启动 OAuth 页面。

这是 Android 端的样子...

AndroidManifest.xml(向 Activity 添加 Intent 过滤器以监听 URI)

<activity android:name=".app.MyActivity"
        android:label="@string/title"
        android:theme="@style/AppTheme"
        android:launchMode="singleTask"
        android:screenOrientation="portrait">

        <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:scheme="mytest" />
        </intent-filter>

    </activity>

Activity.java(启动自定义 Chrome 选项卡)

private void goToSite() {
    CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
    int toolbarColor = getResources().getColor(R.color.primary);
    intentBuilder.setToolbarColor(toolbarColor);vice_key(), shared().getAccessToken());
    intentBuilder.build().launchUrl(this, Uri.parse("https://example.com/some-page"));
}

在我的服务器上,用户填写表单并最终被带到一个页面,该页面应该将他们重定向回应用程序...

server.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<script>
    var redirectToApp = function() {
        setTimeout(function appNotInstalled() {
            window.location.replace("http://example.com/app-not-installed");
        }, 100);
        window.location.replace("mytest://justanexample");
    };
    window.onload = redirectToApp;
</script>
</head>
<body></body>
</html>

但我没有被重定向到该应用程序。我怀疑我的服务器代码有问题,因为在处理 OAuth 页面时可以使用类似的设置( list 和 Activity 代码)。我的服务器代码有什么问题?我该如何解决这个问题?

编辑

Android 端似乎是正确的 - 将服务器代码更改为 <a href="mytest://other/parameters/here">GO TO APP</a>似乎可行,但我需要一种不涉及单击链接的方法

编辑2

上面的代码确实有效:/

因为我位于 Chrome 自定义选项卡中,所以我重定向到了我已经所在的页面(因为 Chroms 自定义选项卡 Activity 被认为是应用内的)。这让我认为没有发生重定向。特里斯坦的评论帮助我意识到了这一点

最佳答案

您可以简单地使用 Javascript,如 This 中所述。回答。

基本上,您将使用

建立链接
<a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">

在哪个包=你的包名

有关详细信息,请参阅此问题 Open android application from a web page

关于android - 如何重定向回 Android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34798144/

相关文章:

android - 处理 xamarin.forms 上的 iOS 和 Android 差异

android - WebViewClient.shouldOverrideUrlLoading 给出无效的 url

android - CoordinatorLayout 和 RecyclerView 问题

android - 在 Android 上创建 emulator-x86 镜像

android - JodaTime:打印周期为天:小时:分钟:秒

PHP:页面未重定向回登录页面

.htaccess - 如何使用重定向更改查询字符串参数的值?

PHP:如何检查用户是否已经登录,否则重定向到登录页面

java - WebView 关闭后不再工作

ios - 为 UIWebView 使用 UIRefreshControl