开发者

Starting a browser from a textbox via Intent - Is http:// required?

开发者 https://www.devze.com 2022-12-31 12:55 出处:网络
I have the following code: /** Open a browser on the URL specified in the text box */ private void openBrowser() {

I have the following code:

    /** Open a browser on the URL specified in the text box */
private void openBrowser() {
    Uri uri = Uri.parse(urlText.getText().toString开发者_如何学JAVA());
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}

When I input "http://www.google.com" to the textbox, it works fine. However, when I try something like "www.google.com" it crashes with:

No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.google.com }

Am I using Uri wrong? Is there a way to extract full address from it? Or am I supposed to write code that adds http manually? e.g, if not starts with http://, add http://.

Thanks!


Am I using Uri wrong?

No. However, www.google.com is not a valid uniform resource identifier.

Or am I supposed to write code that adds http manually?

Yes.

0

精彩评论

暂无评论...
验证码 换一张
取 消