如何重定向回 Android 应用程序?
How to redirect back to Android app?
我需要打开自定义 Chrome 选项卡,将用户带到我的网站,然后将用户重定向回应用程序。它类似于启动 OAuth 页面。
这是 Android 面的样子...
AndroidManifest.xml(将 Intent 过滤器添加到 activity 以侦听 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>
但我没有被重定向到应用程序。我怀疑我的服务器代码有问题,因为类似的设置(清单和 activity 代码)在处理 OAuth 页面时有效。我的服务器代码有什么问题?我该如何解决这个问题?
编辑
Android 方面似乎是正确的 - 将服务器代码更改为 <a href="mytest://other/parameters/here">GO TO APP</a>
似乎可行,但我需要一种不涉及单击 link[=15= 的方法]
编辑 2
上面的代码确实有效:/
因为我在 Chrome 自定义选项卡中,所以我重定向到我已经在的页面(因为 Chroms 自定义选项卡 activity 被认为是在应用程序内)。这让我觉得没有发生重定向。特里斯坦的评论帮助我意识到这一点
您可以按照 This 答案中的说明简单地使用 Javascript。
基本上你会制作一个 link 和一个
<a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">
在哪个包=你的包名
有关详细信息,请参阅此问题Open android application from a web page
我需要打开自定义 Chrome 选项卡,将用户带到我的网站,然后将用户重定向回应用程序。它类似于启动 OAuth 页面。
这是 Android 面的样子...
AndroidManifest.xml(将 Intent 过滤器添加到 activity 以侦听 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>
但我没有被重定向到应用程序。我怀疑我的服务器代码有问题,因为类似的设置(清单和 activity 代码)在处理 OAuth 页面时有效。我的服务器代码有什么问题?我该如何解决这个问题?
编辑
Android 方面似乎是正确的 - 将服务器代码更改为 <a href="mytest://other/parameters/here">GO TO APP</a>
似乎可行,但我需要一种不涉及单击 link[=15= 的方法]
编辑 2
上面的代码确实有效:/
因为我在 Chrome 自定义选项卡中,所以我重定向到我已经在的页面(因为 Chroms 自定义选项卡 activity 被认为是在应用程序内)。这让我觉得没有发生重定向。特里斯坦的评论帮助我意识到这一点
您可以按照 This 答案中的说明简单地使用 Javascript。
基本上你会制作一个 link 和一个
<a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">
在哪个包=你的包名
有关详细信息,请参阅此问题Open android application from a web page