问题描述
我有一个应用,我想给它添加一个分享按钮.单击按钮后,我希望它打开以下窗口:
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"
你能告诉我怎么做吗?
推荐答案
方案一:启动ACTION_SEND Intent
在启动 SEND Intent 时,您通常应该将它包装在一个选择器中(通过 createChooser(Intent, CharSequence)),它将为用户提供适当的界面来选择如何发送您的数据,并允许您指定一个提示,指示它们是什么
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 应用程序 - 添加“共享"按钮在社交网络上分享应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!