简体   繁体   中英

Chrome custom tab crashes if chrome browser is disabled by the user in his device

I am trying to open pdf file in chrome custom tab , it works fine by using this code

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));

But the problem is, if the user have disabled chrome from app settings and there is no other browser it crashes. So how can i open it in the chrome custom tab if it is disabled.

Thanks in advance.

this is the exception that is showing up in the logcat.

 Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://docs.google.com/... (has extras) }

Try this:

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();

try {
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
} 
catch (ActivityNotFoundException e)
{
    // display message to user
}

Handle your exception like below

try{
   CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
   CustomTabsIntent customTabsIntent = builder.build();
   customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
}catch(ActivityNotFoundException e){
   // Handle your exception
}

EDIT

If user has disabled it then you can ask user to enable it from settings .

Code for open phone settings

startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);

REASON behind this, can be easily searched from this SO post . Written by CommonsWare

Chrome does not appear to export that activity, so it cannot be started by third-party apps.

Even if they did, that behavior could easily vary from version to version of Chrome for Android. Google does not document or support access to such activities from any of their commercial apps, let alone Chrome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM