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如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ActionProvider使用示例" android:layout_centerInParent="true" android:textSize="18sp" /> </RelativeLayout>
share_menu.xml如下:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/share_item" android:actionProviderClass="android.widget.ShareActionProvider" android:orderInCategory="100" android:showAsAction="always" android:title="分享" /> </menu>