4

我在使用 ShareActionProvider 时遇到问题。我喜欢通过 ShareActionProvider 共享 webview 的当前 url(可以通过 JS 动态更改 url)。为此,我重写了 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;
4

1 回答 1

4

我知道这有点晚了,但我遇到了同样的问题,我通过在我的 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)

于 2013-10-07T14:41:43.573 回答