Android intent to open user's preferred browser

后端 未结 6 773
梦如初夏
梦如初夏 2020-12-15 08:35

I\'ve been trying to find out how to create an intent that will open the user\'s preferred browser without specifying the URL. I know how to open it by giving a specific URL

6条回答
  •  攒了一身酷
    2020-12-15 08:51

    Try this:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_BROWSER);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent.createChooser(intent, "Select Browser"));
    }
    else { //Popup a msg so as to know the reason of not opening the browser
        Toast.makeText(this, "There is no Browser App", Toast.LENGTH_LONG).show();
    }
    

    Worked for me, hope for you also!

提交回复
热议问题