简体   繁体   English

Android webview 检测特定页面变化

[英]Android webview detect specific page change

I have an android webview app with user authentication, the app allows users to register their devices by collecting the device's android id.我有一个带有用户身份验证的 android webview 应用程序,该应用程序允许用户通过收集设备的 android id 来注册他们的设备。 When a user runs the mobile app, it will check if the mobile device is registered then validates the login details associated with the registered device.当用户运行移动应用程序时,它将检查移动设备是否已注册,然后验证与注册设备关联的登录详细信息。 All works as designed with no issue.所有工作都按设计进行,没有问题。

My problem is that when the PHP session expires, users will be directed to the login page.我的问题是当 PHP session 过期时,用户将被引导到登录页面。 If the user closes the app and re-open it, it will log the user without having to re-enter the login details.如果用户关闭应用程序并重新打开它,它将记录用户,而无需重新输入登录详细信息。 PHP session time-out can not be changed due to company policy.由于公司政策,PHP session 超时无法更改。

I would like to know if I could detect the login page URL change and send the device android id with the login page URL similar to the app startup process.我想知道我是否可以检测到登录页面 URL 更改并发送设备 android id 与登录页面 URL 类似的应用程序启动过程。 So, when the PHP session expires, users will be automatically logged back in without having to re-enter their login credentials.因此,当 PHP session 过期时,用户将自动重新登录,而无需重新输入登录凭据。

Here is the current code I am using, some irrelevant codes have been removed to reduce the code lines.这是我正在使用的当前代码,一些不相关的代码已被删除以减少代码行。 This is my first android app, and I have very limited experience when it comes to mobile programming and I have been searching for the last couple of day without any luck.这是我的第一个 android 应用程序,我在移动编程方面的经验非常有限,而且我最近几天一直在寻找没有任何运气。 Help and directions are highly appreciated.高度赞赏帮助和指导。

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

    boolean isConnct = false;
    // code here to check internet connection

    // end of internet connection code

    if (isConnct){
        // internet is ok
        final WebView webView = (WebView)findViewById(R.id.webView);

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                findViewById(R.id.imageLoading1).setVisibility(ImageView.GONE);
                findViewById(R.id.webView).setVisibility(webView.VISIBLE);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
                // show error message
            }

        });
        // using setWebChromeClient to enable javascript
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                // js confirm code
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                // js alert code
            }
        });
        // enable javascript and other settings
        String url = "https://www.domainname/login/" + android.provider.Settings.Secure.getString(getContentResolver(), Secure.ANDROID_ID);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setSupportZoom(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        webView.loadUrl(url);
    } else {
        // show error message
    }
}

try override shouldOverrideUrlLoading method in WebViewClient , check your login URL and start your auto-login from there尝试覆盖WebViewClient中的shouldOverrideUrlLoading方法,检查您的登录 URL 并从那里开始您的自动登录

another approach is checking cookies expiration time, you may use CookieManager.getInstance() for this另一种方法是检查 cookies 到期时间,您可以为此使用CookieManager.getInstance()

This is what I ended up doing, and it seems working as expected, when PHP session expires, registered devices will be redirected to the index page with new session without having to re-enter user credentials.这就是我最终做的事情,它似乎按预期工作,当 PHP session 到期时,注册的设备将被重定向到索引页面,新的 Z21D6F40CFB511982E4424E0E250A9557 没有重新输入用户凭据。

If there is a logout button then you need to consider adding different procedure for the logout to allow users to logout and re-direct to login page.如果有注销按钮,那么您需要考虑为注销添加不同的程序,以允许用户注销并重定向到登录页面。

webView.setWebViewClient(new WebViewClient(){
                @Override
                public void onPageFinished(WebView view, String url) {
                    //hide loading image
                    findViewById(R.id.imageLoading1).setVisibility(ImageView.GONE);
                    //show webview
                    findViewById(R.id.webView).setVisibility(webView.VISIBLE);
                }

                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
                    // show error message
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if (url.contentEquals("https://www.domainme/login")){
                        String url2 = "https://www.domainme/login/" + android.provider.Settings.Secure.getString(getContentResolver(), Secure.ANDROID_ID);
                        webView.loadUrl(url2);
                        return true;
                    }else {
                        return false;
                    }
                }

// code truncated

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM