7

I want to share text message trough twitter, facebook or in other ways that available on device. I wrote next code:

    Intent share = new Intent(Intent.ACTION_SEND);
    share.putExtra(Intent.EXTRA_TEXT, "Here's some text for Twitter.");
    startActivity(Intent.createChooser(share, "Share this via"));       

But if no apps that can hadle this action, the "no such apps" dialog appears on screen. I want to detect these apps and disable this function if there is no handlers found. How I can do it?

kruz05
  • 501
  • 1
  • 9
  • 17

2 Answers2

24
    Intent intent = new Intent...
    PackageManager manager = mContext.getPackageManager();
    List<ResolveInfo> list = manager.queryIntentActivities(intent, 0);

    if (list != null && list.size() > 0) {
        //You have at least one activity to handle the intent
    } else {
        //No activity to handle the intent.
    }
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
-1
if (intent.resolveActivity(pm) == null) {
    // activity not found
}
xuxu
  • 6,374
  • 1
  • 17
  • 11