"No Activity found to handle Intent" error occurs when its going to load a URL in browser

1

I've checked similar questions about No Activity found to handle Intent error. none of them covered my problem.

I'm getting this error in my Sentry logs when the app is going to open a URL in Chrome browser using Intent in an AppCompatActivity:

android.content.ActivityNotFoundException: No Activity found to handle Intent
{ act=android.intent.action.VIEW dat=https://www.example.com/...
flg=0x10000000 pkg=com.android.chrome }

This is my code:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");

if (MyMethods.isAppInstalled(getApplicationContext(), "com.android.chrome")) {
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        SentryLog.warn(ex);
        // Chrome browser presumably not installed so allow user to choose instead
        intent.setPackage(null);
        startActivity(intent);
    }
} else {
    // Chrome browser presumably not installed so allow user to choose instead
    intent.setPackage(null);
    startActivity(intent);
}

SentryLog.warn(ex); had reported the error.

and this is isAppInstalled() method which is in MyMethods class:

public static boolean isAppInstalled(Context context, String packageName) {
    try {
        if (context != null) {
            context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        }
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        MyLog.w(TAG, new Throwable().getStackTrace()[0].getLineNumber(), e);
        e.printStackTrace();
    }

    return false;
}

sometimes it goes to catch scope. As you see I checked if chrome is installed on device or not, so it's installed if it doesn't go to else! in this situation why it couldn't execute startActivity(intent); and it went to catch scope?

‍ My code is in an Activity, so should I use intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); or not?

android
android-intent
activitynotfoundexception
asked on Stack Overflow Jan 22, 2019 by Alireza Noorali • edited Jan 22, 2019 by Alireza Noorali

3 Answers

0

No need to check chrome it will find browser itself. Notice url must begin with http or https.

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
fragmentActivity.startActivity(browserIntent);
answered on Stack Overflow Jan 22, 2019 by rgaraisayev
0
   try {
        Intent i = new Intent();
        i.setPackage("com.android.chrome");
        i.setAction(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    } catch (Exception e) {
        e.printStackTrace();
        // chrome is not installed in the device
    }

you can do like this way to avoid the crash and also get that app installed or not.

answered on Stack Overflow Jan 22, 2019 by Hemant N. Karmur
0

To check if chrome is installed you can use below method

private boolean isChromeInstalled() {
    PackageInfo pInfo;
    try {
        pInfo = getPackageManager().getPackageInfo("com.android.chrome", 0);
    } catch (PackageManager.NameNotFoundException e) {
        //chrome is not installed on the device
        return false;
    }
    return true;
}
answered on Stack Overflow Jan 22, 2019 by karan

User contributions licensed under CC BY-SA 3.0