Android应用-添加“共享"在社交网络上共享应用程序的按钮 [英] Android app - Adding a "share" button to share the app on social networks

查看:141
本文介绍了Android应用-添加“共享"在社交网络上共享应用程序的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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"

你能告诉我怎么做吗?

推荐答案

解决方案1:启动ACTION_SEND Intent

启动SEND意图时,通常应将其包装在选择器中(通过

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

解决方案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;
}

希望这会有所帮助. :)

Hope this helps. :)

这篇关于Android应用-添加“共享"在社交网络上共享应用程序的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆