本文介绍了使用Android操作栏共享意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用操作栏上的菜单项,我想通过单击共享图标来共享我的应用程序。当我单击共享图标时,它不起作用。另外,我想添加文本,说出共享时安装此应用
。
I'm using a menu item on the action bar and I want to share my app by clicking the share icon. When I click the share icon it doesn't work. Also, I want to add text saying "install this app"
when shared.
这是我的代码:
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
Mainpage.xml菜单:
<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_store"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
推荐答案
如果要静态共享意图
(即,它永远不会改变),然后将您的 onCreateOptionsMenu
更新为
If you want a static share Intent
(i.e., it never changes), then you update your onCreateOptionsMenu
to be
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// 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);
return true;
}
这篇关于使用Android操作栏共享意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!