-3

I need to open the URL in default browser of the phone (not in the application's WebView). Below is my code, but it launches the URL in WebView.

How can I open the URL in the default browser?

Activity2.java

public class Activity2 extends Activity {
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_webview);

      webview=(WebView)findViewById(R.id.webView1);
      webview.setWebViewClient(new MyWebViewClient());
      openURL();
    }

    /** Opens the URL in a browser */
    private void openURL() {
      webview.loadUrl("http://www.XX.org");
      webview.requestFocus();
    }
}
Andrew T.
  • 4,701
  • 8
  • 43
  • 62

1 Answers1

1

No need of webview, If you're planning url to open in broswer then go with the below code

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.XX.org"));
startActivity(i);

If you want to open facebook app from your code:

try {       
    String uri = "facebook://facebook.com/inbox";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    startActivity(intent);
} catch (ActivityNotFoundException e) {
}
Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22
  • I have button facebook, it's possible when user click on button facebook, the page open in facebook application? – user3430800 Apr 14 '15 at 00:46
  • do you want to open facebook app, onclick of button? – Harsha Vardhan Apr 14 '15 at 00:48
  • Yes exactly, I have ImageButton (Facebook) & onClick the page open in the facebook app (not in browser) – user3430800 Apr 14 '15 at 00:51
  • Thanks you Harsha - Your code open just the facebook application but not the page directly. try { String uri = "facebook://facebook.com/MYPAGE_HERE"; Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); startActivity(intent); } catch (ActivityNotFoundException e) { } – user3430800 Apr 14 '15 at 11:36