你的位置:首页 > 操作系统

[操作系统]ShareActionProvider


menu/share_action.

<?

MainActivity

package com.panjn.sharedemo;import android.content.Intent;import android.support.v4.view.MenuItemCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.ShareActionProvider;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends AppCompatActivity {  private Intent shareIntent;  private ShareActionProvider mShareActionProvider;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    shareIntent = new Intent();    shareIntent.setAction(Intent.ACTION_SEND);    shareIntent.putExtra(Intent.EXTRA_TEXT,"panjn.jpeg");    shareIntent.setType("text/plain*");  }  @Override  public boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.share_action,menu);    MenuItem item = menu.findItem(R.id.menu_item_share);    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);    setShareIntent(shareIntent);    return true;  }  private void setShareIntent(Intent intent) {    if(mShareActionProvider != null && intent != null) {      mShareActionProvider.setShareIntent(intent);    }  }}