Android app - Adding a “share” button to share the app on social networks

只谈情不闲聊 提交于 2019-12-04 08:07:48

问题


I have an app, and I'd like to add a share button to it. Once the button is clicked, I'd like it to open the following window:

Then the user will choose where to share it and it will display the following default message: "Just found this great app! Find it here:https://play.google.com/store/apps/details?id=com.ideashower.readitlater.pro"

Can you please tell me how to do it?


回答1:


Solution 1: Launch ACTION_SEND Intent

When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);

# change the type of data you need to share, 
# for image use "image/*"
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, URL_TO_SHARE);
startActivity(Intent.createChooser(intent, "Share"));

Solution 2: Use ShareActionProvider

If you are just looking to add a Share button in Overflow Menu, also have a look at ShareActionProvider.

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.share, menu);
    MenuItem item = menu.findItem(R.id.share_item);
    actionProvider = (ShareActionProvider) item.getActionProvider();

    // Create the share Intent
    String shareText = URL_TO_SHARE;
    Intent shareIntent = ShareCompat.IntentBuilder.from(this)
        .setType("text/plain").setText(shareText).getIntent();
    actionProvider.setShareIntent(shareIntent);
    return true;
}

Hope this helps. :)




回答2:


As explained on Android Developers on this link: http://developer.android.com/training/sharing/shareaction.html

you have to add this menu item:

<item
        android:id="@+id/menu_item_share"
        android:showAsAction="ifRoom"
        android:title="Share"
        android:actionProviderClass=
            "android.widget.ShareActionProvider" />

Then add the following code in Activity:

private ShareActionProvider mShareActionProvider;
...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate menu resource file.
    getMenuInflater().inflate(R.menu.share_menu, menu);

    // Locate MenuItem with ShareActionProvider
    MenuItem item = menu.findItem(R.id.menu_item_share);

    // Fetch and store ShareActionProvider
    mShareActionProvider = (ShareActionProvider) item.getActionProvider();

    // Return true to display menu
    return true;
}

// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
}


来源:https://stackoverflow.com/questions/20236947/android-app-adding-a-share-button-to-share-the-app-on-social-networks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!