Android直接分享

Android direct shared

我正在尝试通过直接分享从我的应用中分享 link。共享对话框必须如下图所示,其中包含消息应用程序中最常用的联系人,例如 WhatsApp 联系人。

这是我用来共享 link:

Intent 结构
 Intent shareIntent = ShareCompat.IntentBuilder
                        .from(getActivity())
                        .setType("text/plain")
                        .setText(sTitle+ "\n" + urlPost)
                        .getIntent();
                if (shareIntent.resolveActivity(
                        getActivity().getPackageManager()) != null)
                    startActivity(shareIntent);

这是我的应用程序显示的内容:

知道如何实现吗?

您应该使用 .createChooserIntent() 而不是 .getIntent()

像下面这段代码,你可以使用Intent.createChooser

            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            Uri screenshotUri = Uri.parse("file://" + filePath);

            sharingIntent.setType("image/png");
            sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
            startActivity(Intent.createChooser(sharingIntent, "Share image using"));

You should use .createChooserIntent() instead of .getIntent()

文档:这使用了 ACTION_CHOOSER 意图,显示了

an activity chooser, allowing the user to pick what they want to before proceeding. This can be used as an alternative to the standard activity picker that is displayed by the system when you try to start an activity with multiple possible matches, with these differences in behavior:

  • You can specify the title that will appear in the activity chooser.
  • The user does not have the option to make one of the matching activities a preferred activity, and all possible activities will always be shown even if one of them is currently marked as the preferred activity.

This action should be used when the user will naturally expect to select an activity in order to proceed. An example if when not to use it is when the user clicks on a "mailto:" link. They would naturally expect to go directly to their mail app, so startActivity() should be called directly: it will either launch the current preferred app, or put up a dialog allowing the user to pick an app to use and optionally marking that as preferred.

In contrast, if the user is selecting a menu item to send a picture they are viewing to someone else, there are many different things they may want to do at this point: send it through e-mail, upload it to a web service, etc. In this case the CHOOSER action should be used, to always present to the user a list of the things they can do, with a nice title given by the caller such as "Send this photo with:".