Home Tags About

How to determine if an Activity exists on the current device

28 Dec 2011
android activity

This code snippet won’t work since startActivity dosen’t throw the exception ActivityNotFoundException:

try {
    startActivity(new Intent(...));
} catch (ActivityNotFoundException e) {
    Toast.makeText(this, "Not installed.", LENGTH_SHORT).show();
}

You maybe want to try this:

public boolean isCallable(Intent intent) {
    return getContext().getPackageManager().resolveActivity(
            intent, PackageManager.MATCH_DEFAULT_ONLY) != null;

    // or(I havn't try it):
    // List list = getPackageManager().queryIntentActivities(
    //         intent, PackageManager.MATCH_DEFAULT_ONLY);
    // return list.size() > 0;
}
</pre>