how to open a URL in default browser onclick?

时间:2016-04-04 16:31:15

标签: android

I have a webview, i want to handle all URL's inside the webview, which is working fine. however, i want to add a button which would get current page's URL and ask user to choose a browser to open the URL in. here is what i have tried so far, but i can't figure out how to pass the URL to the intent, i only know how to get URL-

public void downloadvia (View view, String string) { 
    String uriUrl = webView.getUrl();  
    Intent launchBrowser = new Intent();  
    startActivity(launchBrowser);
    }

obviously it force closes as expected

2 个答案:

答案 0 :(得分:1)

Try this:

Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(uriUrl));
startActivity(launchBrowser);

答案 1 :(得分:0)

Try this:

    Intent browserIntent = new Intent(Intent.ACTION_VIEW);
    browserIntent.setData(Uri.parse(mUrl));
    Intetn chooserIntent = Intent.createChooser(browserIntent, "dialog title");
    startActivity(chooserIntent);

You need to set the action and data of Intent so android will know what exactly you want.