1

My app has a button that leads (should lead, to be precise :) ) to another application's page in the GooglePlay. The button's click reaction is as follows:

public void pressedPurchaseButton(View view)
{
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.infmanrb.multrainer"));
    startActivity(browserIntent);
}        

However, in emulator, button pressing causes an exception:

Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=com.infmanrb.multrainer }

I guess GooglePlay is not installed on Emulator - and that's the reason. But anyway, what is the correct way to avoid application crashing? Can I check in advance whether the Intent will be handled? Or the only way is to try-catch an exception?

Nick
  • 3,205
  • 9
  • 57
  • 108
  • 1
    indeed play is not installed on the emulator. to avoid crash you can either ask the content resolver of simply catch the exception – njzk2 Oct 02 '12 at 10:03

1 Answers1

1

Use a utility method like this :

public static boolean isIntentHandleable(Context context, String action) {
    final PackageManager manager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
        manager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

Pass the intent that you want as action and ofcourse the context reference. If this returns false, this action cannot be handled.

Check this link as well : Check if intent uri is available

Community
  • 1
  • 1
midhunhk
  • 5,560
  • 7
  • 52
  • 83