Is there any way in Android to force open a link to open in Chrome?

后端 未结 10 1112
情书的邮戳
情书的邮戳 2020-11-28 03:37

I\'m currently testing a webapp developed with lots of jQuery animations, and we\'ve noticed really poor performance with the built-in web browser. While testing in Chrome,

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 04:03

    FOllowing up on @philippe_b's answer, I would like to add that this code will not work if Chrome is not installed. There is one more case in which it will not work - that is the case when Chrome is NOT selected as the default browser (but is installed) OR even if no browser is selected as the default.

    In such cases, add the following catch part of the code also.

    try {
        Intent i = new Intent("android.intent.action.MAIN");
        i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
       i.addCategory("android.intent.category.LAUNCHER");
        i.setData(Uri.parse("http://mysuperwebsite"));
        startActivity(i);
    }
    catch(ActivityNotFoundException e) {
    // Chrome is probably not installed 
    // OR not selected as default browser OR if no Browser is selected as default browser
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("somesite.com"));
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
    }
    

提交回复
热议问题