2013-05-20 60 views
4

我遇到了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; 
+0

可能重複http://stackoverflow.com/questions/11627892/onoptionsitemselected-not-called-when-using-actionlayout-sherlockactionbar – zizoujab

+0

這篇文章並沒有爲我工作。我描述了上面的不當行爲。因此我的線程不是重複的。 – janwo

+0

建議使用setOnClickListener(這裏提到的http://stackoverflow.com/a/11628492/1463921)不起作用,因爲我有一個ShareActionProvider-Widget,我想它的行爲有所不同。點擊共享按鈕時,不會觸發setOnClickListener和onOptionsItemSelected。有沒有人有想法? – janwo

回答

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)

+0

你能解釋你是如何實現它的。因爲當我將它添加到onCreateOptionMenu上時,它在onOptionItemSelected上不起作用 – Jigar