2

The app does a login to a web application using WebView. Once in the webview, the webview appears to handle everything for you as it should based on the user's clicks. However I need to review on each event if the URL changes to a specific logout URL.

How can I return the user to the app itself when the user logs out on the web application within the webview? I do not want the webview to stay as the active view.

I have tried WebViewClient.shouldOverrideUrlLoading and View.OnTouchListsner.

The class I tried to implement public but it didn't allow me.

class myWebClient extends WebViewClient
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub

                view.loadUrl(url);
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);


            }
        }

Here's the code before I want to call the method to check the URL;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText phone = (EditText) findViewById(R.id.phone1);
        final EditText login = (EditText) findViewById(R.id.uname);
        final EditText pass = (EditText) findViewById(R.id.password);
        phone.requestFocus();
        final Context context = this;
        submit  = (Button)findViewById(R.id.submit);
        sbutton = (Button)findViewById(R.id.loginCred);
        chcred = (CheckBox) findViewById(R.id.cBox);
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textstring(phone, login, pass);
                encPhone = URLEncoder.encode(Phone);
                encLogin = URLEncoder.encode(Login);
                encPass = URLEncoder.encode(Pass);

                if (chcred.isChecked()) {
                    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
                    prefwrite(editor);
                }
                pushurl();
                clear(phone, login, pass);
            }
        });

        WebViewClient() //Right here is where I want to call it
Jay
  • 127
  • 3
  • 10
  • does the answer on this question help? http://stackoverflow.com/questions/5192314/detect-android-webview-url-location-change – Gelunox Apr 17 '15 at 18:39
  • Thank you all for answering. Here's the issues I'm having following the advice from Jorge. I impmented a class called myWebClient Extends WebViewClient which includes the onPageStarted method as noted in the first answer. 1.) Although I put this within the OnCreate Instace Android Studio gave me an error saying it can't be a public class so I removed the public and just kept the class. Now I want to use this method but don't know how to access it in the class. I edited my initial response above with the code. – Jay Apr 17 '15 at 20:16

2 Answers2

2

This can help you: https://developer.android.com/reference/android/webkit/WebViewClient.html#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap)

The event does not notify you when the url changes, but does when the page starts to load, at that point you only need to verify the current url.

tjpaul
  • 343
  • 3
  • 16
1

Try This webView.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView webview, String url) {

                    Uri uri = Uri.parse(url);
                    if (uri.getScheme().contains("google") || uri.getScheme().contains("tel")) {
                        try {

                            Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                            if (intent.resolveActivity(getPackageManager()) != null)
                                startActivity(intent);
                            return true;
                        } catch (URISyntaxException use) {
                            Log.e("TAG", use.getMessage());
                        }
                    } else {
                        webview.loadUrl(url);
                        Log.e("url", url);
                    }

                    return true;
                }

               @Override
               public void onPageStarted(WebView view, String url, Bitmap favicon) {
                   super.onPageStarted(view, url, favicon);
               }

               @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                }

               @Override
               public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
                   super.doUpdateVisitedHistory(view, url, isReload);
                   String currentUrl=view.getUrl();
                  

               }
           });
           webView.loadUrl(url);

here webView is my WebView and url is the url which I am using in my webview.

If you want to make some changes with the change in url,then go for

@Override public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) { super.doUpdateVisitedHistory(view, url, isReload); String currentUrl=view.getUrl();

               }

you will get the url updation in this call.

XamarinInfo
  • 133
  • 1
  • 8