Android应用 - 添加"分享"按钮以在社交网络上分享应用

dav*_*vid 17 social twitter android share facebook

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

在此输入图像描述

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

你能告诉我怎么做吗?

Ank*_*pli 32

解决方案1:启动ACTION_SEND意图

启动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"));
Run Code Online (Sandbox Code Playgroud)

解决方案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;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.:)