6

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
  • 341
  • 1
  • 8
  • 17

2 Answers2

22
    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
  • 79,170
  • 20
  • 177
  • 186
-1
if (intent.resolveActivity(pm) == null) {
    // activity not found
}
xuxu
  • 5,666
  • 1
  • 14
  • 11