Alireza Noorali
Alireza Noorali

Reputation: 3265

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

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?

Upvotes: 0

Views: 1982

Answers (3)

karan
karan

Reputation: 8843

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;
}

Upvotes: 0

Hemant N. Karmur
Hemant N. Karmur

Reputation: 880

   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.

Upvotes: 0

rgaraisayev
rgaraisayev

Reputation: 396

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);

Upvotes: 0

Related Questions