How can I open a URL in Android's web browser from my application?

前端 未结 30 2727
庸人自扰
庸人自扰 2020-11-21 22:09

How to open an URL from code in the built-in web browser rather than within my application?

I tried this:

try {
    Intent myIntent = new Intent(Int         


        
30条回答
  •  抹茶落季
    2020-11-21 23:03

    a common way to achieve this is with the next code:

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

    that could be changed to a short code version ...

    Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.stackoverflow.com"));      
    startActivity(intent); 
    

    or :

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")); 
    startActivity(intent);
    

    the shortest! :

    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")));
    

    happy coding!

提交回复
热议问题