0

I am opening the devices browser to a specific url from an intent as follows:

String url = "http://www.google.com";
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

How can I now validate that that the page was loaded successfully in the browser? Is there a way to return the loading state or contents of the browser app?

DAC
  • 231
  • 4
  • 12

2 Answers2

0

You can try this

try
{

    String url = "http://www.google.com";
    Uri uri = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);

}
catch (MalformedURLException e) 
{
              e.printStackTrace();
}

Hope that helps

Kelo Adler
  • 13
  • 3
  • Thanks, but I'm not looking to catch exceptions when starting the intent. I'm looking for a way to check that the url is done loading in the web browser successfully. So for example, if I enter www.badurl.com, the browser will load but give me a error in the browser as its not a real url. – DAC Oct 16 '14 at 15:18
  • And if you try making him ping the url before sending the intent? try with this http://stackoverflow.com/questions/6381306/ping-url-and-get-status-in-java – Kelo Adler Oct 16 '14 at 21:03
0

You can achieve that implementing a WebViewClient with the methods onPageFinished()or onReceivedError() :

    myWebView.setWebViewClient(new WebViewClient() {
       public void onPageFinished(WebView view, String url) {
            Log.i("WebView", "Page succesfully loaded!");
        }
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Log.e("WebView", "An error ocurred: " + description + " loading the page:" + failingUrl + ", with code" + errorCode);       }

    });

Mor information about possible Error Codes: http://developer.android.com/reference/android/webkit/WebViewClient.html#Summary

More info:

WebViewClient

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • How do you initialize myWebView, I'm looking at the docs but it doesn't seem to be working for me. In my case the browser being opened is Chrome, if that makes a difference. – DAC Oct 16 '14 at 18:49