How to detect android apps that can handle request

2019-06-20 07:58发布

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?

2条回答
姐就是有狂的资本
2楼-- · 2019-06-20 08:01
    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.
    }
查看更多
唯我独甜
3楼-- · 2019-06-20 08:20
if (intent.resolveActivity(pm) == null) {
    // activity not found
}
查看更多
登录 后发表回答