gpt4 book ai didi

android - 判断是否触发 ShareActionProvider

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:39 28 4
gpt4 key购买 nike

我在使用 ShareActionProvider 时遇到问题。我喜欢通过 ShareActionProvider 共享 webview 的当前 url(url 可以通过 JS 动态更改)。为此,我覆盖了 onOptionsItemSelected 以通过 setShareIntent 更改 Intent。但是当我单击 ShareActionProvider 并通过特定应用程序共享它时,没有任何反应。我调试了它,发现没有触发 onOptionsItemSelected。有人可以帮帮我吗?

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.game, menu);

MenuItem mShareActionProviderItem = (MenuItem) menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) mShareActionProviderItem.getActionProvider(); // is a field
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
mShareActionProvider.setShareIntent(sendIntent); //needed to be able to click on Item

mShareActionProviderItem.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl();

/*ShareButton set URL*/
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url));
mShareActionProvider.setShareIntent(sendIntent);
}
});
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_share:
/*URL holen*/
String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl();

/*ShareButton mit URL bestücken*/
ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url));
mShareActionProvider.setShareIntent(sendIntent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;

最佳答案

我知道有点晚了,但我遇到了同样的问题,我通过在我的 ShareActionProvider 对象上定义一个 OnShareTArgetSelectedListener 来解决它。 :

        mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {

@Override
public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) {

return false;
}
});

http://developer.android.com/reference/android/widget/ShareActionProvider.html#setOnShareTargetSelectedListener(android.widget.ShareActionProvider.OnShareTargetSelectedListener)

关于android - 判断是否触发 ShareActionProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16654718/

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