如何发送一个响应返回到原生的Andr​​oid应用Andr、oid

2023-09-07 13:52:35 作者:淡淡的忧伤

我有样原生Android应用程序。用户创建该用户想要访问该应用程序的轮廓。根据应用程序的设置,用户将被重定向到一个特定的网址在浏览器认证或显示在本机应用程序登录屏幕。如果用户被重定向到浏览器的URL(我使用的是的WebView ),我要响应发送回本机应用程序有关身份验证的状态。我怎么做?我如何添加监听到开始BrowserActivity意图是什么?

I have sample native android app. User creates a profile for the application that user wants to access. Based on application set up, user will be redirected to a particular URL in browser for authentication OR shown a login screen in native app. If user is redirected to an URL in browser ( i am using a webview), I want to send the response back to native app about the status of authentication. How do I do that? How can I add listener to intent which is starting BrowserActivity?

推荐答案

寻找一些在服务器的身份验证尝试的反应是一致的的WebView 可以检测和分支上

Look for something consistent in the server's response to the authentication attempt that the WebView can detect and branch on.

例如,谷歌的OAuth结果放置在<标题> HTML响应的元素,让你解析,并返回在给调用活动的响应 WebChromeClient onReceivedTitle 处理程序:

For example, Google OAuth puts the results in the <title> element of the HTML response, so you parse that and return a response to the calling activity in a WebChromeClient onReceivedTitle handler:

public class MyChrome extends WebChromeClient {
    public void onReceivedTitle(WebView view, String title) {
        final Intent i = new Intent();
        if (title.startsWith("Denied")) {
            i.putExtra("auth2code", "");
            setResult(RESULT_OK, i);
            finish();
        } else if (title.contains("code=")) {
            final String[] stuff = title.split("=");
            if (stuff.length > 1) {
                auth2code = stuff[1];
                i.putExtra("auth2code", auth2code);
            } else {
                i.putExtra("auth2code", "");
            }
            setResult(RESULT_OK, i);
            finish();
        }
    }
}

(在的onCreate 你需要这样说 webView.setWebChromeClient(新MyChrome());

这个例子说明了基本的方法,但它必须修改基于任何特定的服务实际上如何响应认证尝试。

This example shows the basic method, but it has to be modified based on how any given service actually responds to an authentication attempt.

 
精彩推荐
图片推荐