繁体   English   中英

添加“分享”按钮以在社交网络上分享应用

[英]Adding a "share" button to share the app on social networks

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

在此处输入图像描述

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

解决方案 1:启动 ACTION_SEND Intent

启动 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"));

解决方案 2:使用 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;
}

希望这可以帮助。 :)

正如此链接上的 Android 开发人员所解释的:http: //developer.android.com/training/sharing/shareaction.html

您必须添加此菜单项:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM