如何检测可以处理请求的Intent操作的Android应用程序?

kru*_*z05 6 android android-intent

我想通过twitter,facebook或设备上提供的其他方式分享短信.我写了下一个代码:

    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"));       
Run Code Online (Sandbox Code Playgroud)

但是,如果没有可以执行此操作的应用程序,屏幕上会显示"无此类应用程序"对话框.如果找不到处理程序,我想检测这些应用程序并禁用此功能.我怎么能这样做?

Rag*_*ood 20

    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.
    }
Run Code Online (Sandbox Code Playgroud)