开发者

How to implement the SHARE function of a Android app?

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.

For example, I have SpringPad installed开发者_StackOverflow社区 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.


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)
        }


If you want to add your application in this lists you need to declare <intent-filter> in app's AndroidManifest.xml and in your target activity in onCreate() you must handle this intent.

Example (I get it from Email app and edited a little).

Docs about Intents and Intent Filters

About intent-filter as element of AndroidManifest

For example realisation see source of Android Email application (files AndroidManifest.xml and src/com/android/email/activity/MessageCompose.java (in onCreate())).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜