添加分享按钮后,点击如何弹出下拉菜单显示可以分享的应用。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//return super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
shareActionProvider = (android.support.v7.widget.ShareActionProvider) MenuItemCompat.getActionProvider(item);
return true;
}
private void setshareIntent(Intent shareIntent){
if (shareActionProvider!=null){
shareActionProvider.setShareIntent(shareIntent);
}
}
private Intent createIntent(){
Intent shareIntent = new Intent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,"hello");
return shareIntent;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//return super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.refresh:
updateWeather();
break;
case R.id.setting:
Intent intent = new Intent(MyApplication.getContext(),SettingsActivity.class);
startActivity(intent);
break;
case R.id.menu_item_share:
setshareIntent(createIntent());
default:
break;
}
return super.onOptionsItemSelected(item);
}
MainActicty继承AppCompatActivity
menu.xml文件
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orderInCategory="100"
android:title="刷新" />
android:orderInCategory="100"
android:title="设置"/>
android:orderInCategory="100"
android:title="位置"/>
android:id="@+id/menu_item_share"
app:showAsAction="ifRoom"
android:title="share"
android:icon="@drawable/chrome48"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>