一丶代码见本文
二丶课程讲解
这里通过单击事件演示
public class Main1223Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1223); } //当单击按钮时,触发,自动调用系统分享程序列表 public void shareTextView(View view) { Intent sendIntent = new Intent(); //作用 sendIntent.setAction(Intent.ACTION_SEND); //类型 sendIntent.setType("text/plain"); //分享的内容 sendIntent.putExtra(Intent.EXTRA_TEXT,"我宣你"); //启动系统的分享程序 startActivity(sendIntent); } }
//指定选择器:参数:intent ,分享列标题 // startActivity(Intent.createChooser(sendIntent, "您好啊"));
//分享图片: public void sharedImage(View view){ //图片路径 Uri uriImage = Uri.parse("./sdcard/a.jpg"); Intent sendIntent = new Intent(); //作用 sendIntent.setAction(Intent.ACTION_SEND); //类型 sendIntent.setType("image/*"); //分享的内容 sendIntent.putExtra(Intent.EXTRA_STREAM, uriImage); startActivity(Intent.createChooser(sendIntent,"分享图片到")); }
public void multImage(View view){ //图片路径 Uri uriImage1 = Uri.parse("./sdcard/a.jpg"); Uri uriImage2 = Uri.parse("./sdcard/b.jpg"); ArrayList<Uri> list = new ArrayList<Uri>(); list.add(uriImage1); list.add(uriImage2); Intent sendIntent = new Intent(); //作用 sendIntent.setAction(Intent.ACTION_SEND_MULTIPLE); //类型 sendIntent.setType("image/*"); //分享的内容 sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,list); startActivity(Intent.createChooser(sendIntent, "分享图片到")); }
这里要注意清单文件配置:
//自已的app接收数据:会自动在分享列表程序中显示自已的app, public class ReceiverShared extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_receiver_shared); textView = (TextView) findViewById(R.id.content); handlerContent(); } private void handlerContent() { Intent intent = getIntent(); String type = intent.getType(); String action = intent.getAction(); if(Intent.ACTION_SEND.equals(action)){ if("text/plain".equals(type)){ //获取内容: String info = intent.getStringExtra(Intent.EXTRA_TEXT); textView.setText(info); } } } } 清单配置: //接收数据的activity: <activity android:name=".ReceiverShared" android:label="@string/title_activity_receiver_shared" > <intent-filter> <action android:name="android.intent.action.SEND"></action> <category android:name="android.intent.category.DEFAULT"></category> <data android:mimeType="text/plain"/> </intent-filter> </activity>
这个比较实用,每个项目应该都少不了这个
menu添加
<item android:id="@+id/shares" android:orderInCategory="200" android:title="分享" app:actionProviderClass="android.support.v7.widget.ShareActionProvider" app:showAsAction="ifRoom"/>主文件添加
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main1223, menu); MenuItem item = menu.findItem(R.id.shares); //一定是V7下的包 ShareActionProvider sp = (ShareActionProvider) MenuItemCompat.getActionProvider(item); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain"); //分享的内容 intent.putExtra(Intent.EXTRA_TEXT, "你好啊1"); sp.setShareIntent(intent); return super.onCreateOptionsMenu(menu); }演示效果: