gpt4 book ai didi

android - 使用 Android 操作栏分享 Intent

转载 作者:太空狗 更新时间:2023-10-29 15:31:01 27 4
gpt4 key购买 nike

我正在使用操作栏上的菜单项,我想通过单击共享图标来共享我的应用程序。当我单击共享图标时,它不起作用。另外,我想在分享时添加文字 "install this app"

这是我的代码:

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>

最佳答案

如果您想要一个静态共享 Intent(即,它永远不会改变),那么您可以将 onCreateOptionsMenu 更新为

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 - 使用 Android 操作栏分享 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17928709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com