Last Updated: February 25, 2016
·
1.272K
· munday

Checking for a receiver of an intent before calling startActivity

Function:

public boolean isCallable(Intent intent) {
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

Usage:

final Intent LaunchIntent = getPackageManager().("com.android.vending");
if(isCallable(LaunchIntent)){
    startActivity(LaunchIntent);
}else{
    Toast t = Toast.makeText(this, "No play store!", Toast.LENGTH_LONG);
    t.setGravity(Gravity.CENTER, 0, 0);
    t.show();
}