我和shareactionprovider有问题。我喜欢通过shareactionprovider共享webview的当前url(url可以通过js动态更改)。为此,我重写了optionsitemselected以通过setshareintent更改意图。但当我点击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)

10-07 18:58