Need help opening a url in the phone's default browser

こ雲淡風輕ζ 提交于 2019-12-23 03:16:25

问题


I'm trying to make test-program that opens a website by pushing a button. I have made a listener and everything, my problem is just that, the url-opening-script makes an error and tells me "Uri cannot be resolved". Here's the script I use to open up an webpage:

Intent browserIntent = new Intent("android.intent.action.VIEW",Uri.parse("http://www.google.com"));  
startActivity(browserIntent);

I have the import android.app.Activity;, import android.content.Intent; and import android.content.ContentUris; (maybe the last one isn't needed) so I can't find out what's wrong...

I hope someone knows what I've done wrong, or forgot to import...

P.S. I've used this pages to get to where I am:

How can I open a URL in Android's web browser from my application?
Sending an Intent to browser to open specific URL


回答1:


Import the Uri class. If you are using Eclipse you can hit ctrl-shift-o to add needed imports, and remove unused ones.




回答2:


try this
String url = "http://www.google.com";
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);

if you want import class then Click CTRL+Sift+O to import all required packages in eclipse.



来源:https://stackoverflow.com/questions/3204305/need-help-opening-a-url-in-the-phones-default-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!