如何实现一个Android应用程序的分享功能?如何实现、应用程序、功能、Android

2023-09-06 01:10:16 作者:骑着蜗牛闯红灯

有时候,我们可以看到,一些点击分享按钮,可共享的方式显示一个列表之后。这名单似乎动态生成的,而不是硬codeD。

Sometimes we can see that after clicking some "share" button, a list of sharable ways displays. And that list seems generated dynamically and not hard-coded.

例如,我已经安装在我的手机SpringPad,以及一些应用程序共享功能是能够通过共享的SpringPad内容,但它怎么会知道我有SpringPad?

For example, I have SpringPad installed on my phone, and some apps' sharing function is able to share the content via SpringPad, but how could it know that I have SpringPad?

如何实现这样的功能?谢谢你。

How to implement such a function? Thanks.

推荐答案

下面是我的Andr​​oid应用程序BBC新闻,这是否实际code。这股的URL页面。如果用户安装了Facebook和Twitter的应用程序,他们将提供机会分享这些服务,以及电子邮件等。

Here is the actual code from my Android app BBC News that does this. It shares the URL to the page. If the user has Facebook or Twitter apps installed they will be offered the opportunity to share to those services, as well as email etc.

        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, pageUrl);

        try {
          startActivity(Intent.createChooser(intent, "Select an action"));
        } catch (android.content.ActivityNotFoundException ex) {
          // (handle error)
        }