Android-如何实现带有消息的共享按钮?
我正在尝试实现一个简单的共享操作,向用户显示Facebook、twitter和电子邮件的菜单,在那里他们可以共享我预先设置的一组消息,比如“我正在使用一个很棒的应用程序” 到目前为止,我掌握的是样式:Android-如何实现带有消息的共享按钮?,android,Android,我正在尝试实现一个简单的共享操作,向用户显示Facebook、twitter和电子邮件的菜单,在那里他们可以共享我预先设置的一组消息,比如“我正在使用一个很棒的应用程序” 到目前为止,我掌握的是样式: <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_share" android:title="@string/sh
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_share"
android:title="@string/share"
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
但这只是打开电子邮件。但是我如何打开包含所有可能共享文本和链接的位置的菜单呢
谢谢
String message = "I am using a great app on http://www.xyz.com"
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(i, "Share with friends"));
但这只是打开了电子邮件
那么,您使用此意图的时间不正确,或者设备上唯一一个广告支持text/plain
的ACTION\u SEND
的应用程序是“email”
但是我如何打开包含所有可能共享文本和链接的位置的菜单呢
我的Galaxy Nexus上运行的以下活动显示了8个可能的共享目的地,其中一些是内置的(蓝牙、消息),另一些是第三方的(记住Milk、Evernote):
注意,当要共享的文本准备好时,您需要在ShareActionProvider
上调用setShareContent()
。在我的例子中,这来自EditText
小部件,因此每当文本更改时都需要更新,这就是为什么我在onTextChanged()中调用setShareContent()
是否有facebook、twitter应用程序可用?事实上,我想我的困惑在于如何将菜单xml连接到意图。谢谢-查看并尝试它…将在几分钟后报告结果:)顺便问一下,SherlockFragmentActivity是什么-我应该也使用它吗?@Genadinik:这是基础为使用ActionBarSherlock和fragments backport的活动初始化。由于我在这里不使用fragments,SherlockActivity
在您仅使用ActionBarSherlock时可以工作。如果您的应用程序仅支持API级别11+,您将使用Activity
。出于某种原因,我的IDE无法识别SherlockFragmentActivity和FragmentActivity.是否有我需要添加的库?@Genadinik:这些是ActionBarSherlock的一部分。ActionBarSherlock是第三方库,提供了操作栏的一个后端口。您需要在昨天决定是否计划支持API级别10(或更低),或者不。在做出此决定之前,您不应使用ShareActionProvider
执行任何操作。然后,也只有在那时,您才能确定是否应下载和使用ActionBarSherlock。
String message = "I am using a great app on http://www.xyz.com"
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(i, "Share with friends"));
public class MainActivity extends SherlockFragmentActivity implements
ShareActionProvider.OnShareTargetSelectedListener, TextWatcher {
private ShareActionProvider share=null;
private Intent shareIntent=new Intent(Intent.ACTION_SEND);
private EditText editor=null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
shareIntent.setType("text/plain");
editor=(EditText)findViewById(R.id.editor);
editor.addTextChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.actions, menu);
share=
(ShareActionProvider)menu.findItem(R.id.share)
.getActionProvider();
share.setOnShareTargetSelectedListener(this);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onShareTargetSelected(ShareActionProvider source,
Intent intent) {
Toast.makeText(this, intent.getComponent().toString(),
Toast.LENGTH_LONG).show();
return(false);
}
@Override
public void afterTextChanged(Editable s) {
shareIntent.putExtra(Intent.EXTRA_TEXT, editor.getText());
share.setShareIntent(shareIntent);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// ignored
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// ignored
}
}