我在实现菜单以共享我的应用程序时遇到问题。当我打开具有图标 share_button 的活动时,会立即显示“共享方式”对话框。我认为我对这行代码“startActivity(Intent.createChooser(shareIntent(),“Share ...”))有问题;”
这是我的代码
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareHistoryFileName(null);
// Create the share Intent
String playStoreLink = "https://play.google.com/store/apps/details?id=" +
getPackageName();
String yourShareText = "Install this app " + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
mShareActionProvider.setShareIntent(shareIntent);
startActivity(Intent.createChooser(shareIntent(), "Share via"));
return true;
}
菜单项
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:icon="@drawable/ic_share"
android:actionProviderClass="android.widget.ShareActionProvider" />