在用户成功登录(WebView)后关闭活动

Close activity after successful user login (WebView)

本文关键字:活动 WebView 用户 成功 登录      更新时间:2023-09-26

我正在制作一个Android移动应用程序,它使用第三方登录页面在WebView中加载身份验证。当用户输入username/pass并提交表单时,它将cookie保存在用户的设备上并将用户重定向到下一页。

我想在这里做的是防止重定向,在cookie保存到用户的设备后关闭登录活动,并向用户显示主要活动。

任何建议吗?谢谢。

谢谢你的快速回答。我找到了一个适合我的解决方案。

正如我在问题中解释的那样,在用户输入凭据并成功登录后,WebView将加载下一页。我所做的是捕获重定向并关闭该活动。像这样:

myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("https://www.someservice/nextpage")) {
            Intent i = new Intent(LogInActivity.this,MainActivity.class);
            startActivity(i);
            finish();
        }
        return true;
    }
}

我不确定这是否是最优雅的解决方案,但它很短,它适合我:)

为WebView使用一个单独的activity ..

@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
    if (url.contains("http://redirectingurl")
        finish();  // close activity
    else
        view.loadUrl(url);
    return true; 
}
我没有试过,但我认为这将工作。选择依据

我现在不能发布所有的代码,但我会给你一个想法。

onPageFinished(Webview view, STring url)方法,你可以注入一个javascript文件,控制login按钮在第三方网页,像这样:

@Override
public void onPageFinished(WebView view, String url) {
    view.loadUrl("javascript:/injection.js content will be here/")
}

injection.js

document.getElementById("loginButton").addEventListener("click", function(){
    //login process copy paste without redirection
    loginMethod()
});

并添加一个JavaScriptInterface到webView:

mWebView.addJavascriptInterface(new JsInterfaceLogin(this.getContext()), "MY_APP");

JsInterfaceLogin:

public class JsInterfaceLogin {
    private final Context context;
    public JsInterfaceLogin(Context context) {
       this.context = context;
    }
    @JavascriptInterface
    public void ___loggedIn___(String toast) {
       Toast.makeText(context, ""+toast, Toast.LENGTH_SHORT).show();
       //When user logged in, you can detect it in here
    }
}

在injection.js的loginMethod()函数中,在登录过程后调用:

window.MY_APP.___loggedIn___("test")

用户成功登录后将关闭登录活动并启动新活动

It is working smooth ....我申请了。

package com.loginsql.afterloginmove;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private WebView webViewLogin;
    private TextView loginTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webViewLogin = findViewById(R.id.myWebView);
        loginTextView = findViewById(R.id.logintext);

        webViewLogin.setWebViewClient(new WebViewClient());
        webViewLogin.setWebChromeClient(new WebChromeClient());
        webViewLogin.getSettings().setJavaScriptEnabled(true);
        webViewLogin.loadUrl("https://myweblogin.com/auth/index");
        webViewLogin.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains(url)) {
                    Intent i = new Intent(MainActivity.this, DashboardActivityActivity.class);
                    startActivity(i);
                    finish();
                }
                return true;
            }
        });
    }
}