我正在尝试创建一个简单的共享菜单,列出用于共享文本的各种应用程序。我遵循与android相同的方法。 这是我的menuactivity
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection.
switch (item.getItemId()) {
case R.id.read_aloud_menu_item:
System.out.println("goes itno read aloud case");
System.out.println(item.getItemId());
mSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, null);
//mSpeech.speak("Hello how are you", TextToSpeech.QUEUE_FLUSH, null);
mSpeech.setLanguage(Locale.US);
System.out.println(item.getItemId());
return true;
case R.id.share:
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
//sharejson(item.getItemId());
Intent in=new Intent(Intent.ACTION_SEND);
in.setAction(Intent.ACTION_SEND);
in.setType("text/plain");
in.putExtra(Intent.EXTRA_TEXT, "extra text that you want to put");
mShareActionProvider.setShareIntent(in);
//startActivity(Intent.createChooser(in, "Share via"));
return true;
}
}
我的xml
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/read_aloud_menu_item"
android:title="@string/read_aloud"
android:icon="@drawable/ic_reply_50" />
<item android:id="@+id/share"
android:title="@string/share"
android:icon="@drawable/ic_reply_50"
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
共享选项显示在列表中,但单击菜单选项时没有任何反应。我也尝试过createchooser,但这也是徒劳的。 createChooser给出错误&#34;没有应用程序可以执行此操作&#34;
答案 0 :(得分:0)
使用此
case R.id.share:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,
"Application.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
return true;
在xml中:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<group android:id="@+id/group" >
<item
android:id="@+id/action_share"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
android:icon="@drawable/share"
android:title="Share"
yourapp:showAsAction="ifRoom"/>
</group>
答案 1 :(得分:0)
使用此代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_settings:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Application.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
break;
default:
break;
}
return true;
}
和xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
这对我有用