Android 应用程序 - 添加“共享”按钮以在社交网络上共享应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20236947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:30:33  来源:igfitidea点击:

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

androidfacebooktwittersharesocial

提问by david

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:

我有一个应用程序,我想为它添加一个共享按钮。单击按钮后,我希望它打开以下窗口:

enter image description here

在此处输入图片说明

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"

然后用户将选择在哪里分享它,它会显示以下默认消息:“刚刚找到这个很棒的应用程序!在这里找到它:https: //play.google.com/store/apps/details?id=com.ideashower .readitlater.pro"

Can you please tell me how to do it?

你能告诉我怎么做吗?

回答by Ankit Popli

Solution 1: Launch ACTION_SEND Intent

解决方案 1:启动 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.

在启动 SEND 意图时,您通常应该将它包装在一个选择器中(通过createChooser(Intent, CharSequence)),这将为用户提供适当的界面来选择如何发送您的数据,并允许您指定一个提示,指示他们的内容是做。

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

解决方案 2:使用 ShareActionProvider

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

如果您只是想在溢出菜单中添加共享按钮,还可以查看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. :)

希望这可以帮助。:)

回答by Reijer

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

正如 Android 开发人员在此链接上所解释的: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:

然后在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);
    }
}