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

前端 未结 30 3268
庸人自扰
庸人自扰 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 22:48

    android.webkit.URLUtil has the method guessUrl(String) working perfectly fine (even with file:// or data://) since Api level 1 (Android 1.0). Use as:

    String url = URLUtil.guessUrl(link);
    
    // url.com            ->  http://url.com/     (adds http://)
    // http://url         ->  http://url.com/     (adds .com)
    // https://url        ->  https://url.com/    (adds .com)
    // url                ->  http://www.url.com/ (adds http://www. and .com)
    // http://www.url.com ->  http://www.url.com/ 
    // https://url.com    ->  https://url.com/
    // file://dir/to/file ->  file://dir/to/file
    // data://dataline    ->  data://dataline
    // content://test     ->  content://test
    

    In the Activity call:

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(URLUtil.guessUrl(download_link)));
    
    if (intent.resolveActivity(getPackageManager()) != null)
        startActivity(intent);
    

    Check the complete guessUrl code for more info.

提交回复
热议问题