Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) }
like image 245
Anmol317 Avatar asked Oct 25 '25 02:10

Anmol317


2 Answers

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
}
like image 89
Vishal Yadav Avatar answered Oct 26 '25 17:10

Vishal Yadav


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.

like image 29
Rahul Khurana Avatar answered Oct 26 '25 16:10

Rahul Khurana