MainActivity
package cc.testsimpleactionbar6;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.MenuItem;import android.widget.ShareActionProvider;/** * Demo描述: * ActionProvider使用示例 * 在此以ShareActionProvider为例 * * 备注说明: * 在menu的xml文件中需要设置属性: * android:actionProviderClass="android.widget.ShareActionProvider" * * 参考资料: * 1 http://docs.eoeandroid.com/training/sharing/shareaction.html * 2 http://blog.csdn.net/eclipsexys/article/details/8688538 * 3 http://blog.csdn.net/think_soft/article/details/7370686 * Thank you very much * */public class MainActivity extends Activity { private ShareActionProvider mShareActionProvider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.share_menu, menu); MenuItem menuItem = menu.findItem(R.id.share_item); mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider(); Intent shareIntent=getShareIntent(); mShareActionProvider.setShareIntent(shareIntent); return true; } public Intent getShareIntent() { Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, "这里是要分享的文字"); intent.setType("text/plain"); Intent.createChooser(intent, "Share"); return intent; }}
main.xml
share_menu.xml