我遇到了ShareActionProvider問題。我喜歡通過ShareActionProvider共享webview的當前網址(url可以通過JS動態更改)。爲此,我重寫了onOptionsItemSelected以通過setShareIntent更改Intent。但是當我點擊ShareActionProvider並通過特定的應用程序共享它時,什麼也沒有發生。我調試了它,發現onOptionsItemSelected沒有被觸發。有人可以幫我嗎?確定ShareActionProvider是否被觸發
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;
可能重複http://stackoverflow.com/questions/11627892/onoptionsitemselected-not-called-when-using-actionlayout-sherlockactionbar – zizoujab
這篇文章並沒有爲我工作。我描述了上面的不當行爲。因此我的線程不是重複的。 – janwo
建議使用setOnClickListener(這裏提到的http://stackoverflow.com/a/11628492/1463921)不起作用,因爲我有一個ShareActionProvider-Widget,我想它的行爲有所不同。點擊共享按鈕時,不會觸發setOnClickListener和onOptionsItemSelected。有沒有人有想法? – janwo