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

后端 未结 10 1113
情书的邮戳
情书的邮戳 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:06

    A more elegant way to achieve this is to use the Intent.ACTION_VIEW intent as normal, but add the package com.android.chrome to the intent. This works regardless of whether Chrome is the default browser and ensures exactly the same behavior as if the user had selected Chrome from the chooser list.

    String urlString = "http://mysuperwebsite";
    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage("com.android.chrome");
    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        // Chrome browser presumably not installed so allow user to choose instead
        intent.setPackage(null);
        context.startActivity(intent);
    }
    

    Update

    For Kindle Devices:

    Just in case if you want to open Amazon Default Browser in case chrome app is not installed in Amazon Kindle

    String urlString = "http://mysuperwebsite";
    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage("com.android.chrome");
    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        // Chrome browser presumably not installed and open Kindle Browser
        intent.setPackage("com.amazon.cloud9");
        context.startActivity(intent);
    }
    

提交回复
热议问题