APP分享
---恢复内容开始---
发送简单文本:
- 意图允许用户快速而方便地共享信息
- Intent sendIntent = new Intent();
- sendIntent.setAction(Intent.ACTION_SEND);
- sendIntent.putExtra(Intent.EXTRA_TEXT,"This is my text to send");
- sendIntent.setType("text/plain");
- startActivity(sendIntent);
- 指定选择器当有多个时可以指定:startActivity(Intent.createChooser(sendIntent,getResources().getText(R.sting.send_to)));
- 点击按钮分享:
1 public void share_text(View view) { 2 Intent text_it = new Intent(); 3 /*标准发送,qq,微信,等都以匹配到*/ 4 text_it.setAction(Intent.ACTION_SEND); 5 text_it.setType("text/plain"); 6 text_it.putExtra(Intent.EXTRA_TEXT,"你要发送的内容"); 7 // startActivity(text_it); 8 startActivity(Intent.createChooser(text_it,"你要发送的标题")); 9 }
发送二进制内容:
1 /*二进制:分享图片为例*/ 2 public void share_img(View view) { 3 Uri uri = Uri.parse("/sdcard/1.jpg"); 4 Intent img_it = new Intent(); 5 /*标准发送,qq,微信,等都以匹配到*/ 6 img_it.setAction(Intent.ACTION_SEND); 7 /* *:所有格式*/ 8 img_it.setType("image/*"); 9 img_it.putExtra(Intent.EXTRA_STREAM,uri); 10 // startActivity(text_it); 11 startActivity(Intent.createChooser(img_it,"你要分享的图片标题")); 12 }
发送多件内容:
1 /*分享多文件:分享多个图片为例*/ 2 public void share_imgs(View view) { 3 Uri uri1 = Uri.parse("/sdcard/1.jpg"); 4 Uri uri2 = Uri.parse("/sdcard/2.jpg"); 5 Uri uri3 = Uri.parse("/sdcard/3.jpg"); 6 ArrayList<Uri> list = new ArrayList<>(); 7 list.add(uri1); 8 list.add(uri2); 9 list.add(uri3); 10 Intent imgs_it = new Intent(); 11 imgs_it.setAction(Intent.ACTION_SEND_MULTIPLE); 12 /* *:所有格式*/ 13 imgs_it.setType("image/*"); 14 imgs_it.putParcelableArrayListExtra(Intent.EXTRA_STREAM,list); 15 // startActivity(text_it); 16 startActivity(Intent.createChooser(imgs_it,"你多个文件分享的图片标题")); 17 }
ShareActionProvider:
在onCreateOptionsMenu方法中:
1 @Override 2 public boolean onCreateOptionsMenu(Menu menu) { 3 getMenuInflater().inflate(R.menu.menu,menu); 4 MenuItem menuItem = menu.findItem(R.id.shared); 5 ShareActionProvider sap = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); 6 Intent intent = new Intent(); 7 intent.setAction(Intent.ACTION_SEND); 8 intent.setType("text/plain"); 9 intent.putExtra(Intent.EXTRA_TEXT,"您要分享的内容"); 10 sap.setShareIntent(intent); 11 return super.onCreateOptionsMenu(menu); 12 }
Menu中文件menu.xml:注意导入v7包:
1 <?xml version="1.0" encoding="utf-8"?> 2 <menu xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto"> 4 <item android:id="@+id/action_setting" 5 android:title="Setting" 6 android:orderInCategory="100" 7 app:showAsAction="never"/> 8 <item 9 android:id="@+id/shared" 10 android:title="分享" 11 android:orderInCategory="200" 12 app:showAsAction="ifRoom" 13 app:actionProviderClass="android.support.v7.widget.ShareActionProvider" 14 /> 15 </menu>
接收其他应用发出的内容:
在组件中清单文件的设置:
1 <!--接收对象信息设置--> 2 <activity android:name=".ReceiverShareMainActivity"> 3 <intent-filter> 4 <action android:name="android.intent.action.SEND"/> 5 <category android:name="android.intent.category.DEFAULT"/> 6 <data android:mimeType="text/plain"/> 7 </intent-filter> 8 </activity>
组件实例:
1 public class ReceiverShareMainActivity extends AppCompatActivity { 2 private TextView content ; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_receiver_share_main); 7 content = (TextView) findViewById(R.id.content); 8 handlerContent(); 9 } 10 private void handlerContent() { 11 Intent intent = getIntent() ; 12 /*根据类型不同做出相应的操作*/ 13 String type = intent.getType(); 14 String action = intent.getAction(); 15 if (Intent.ACTION_SEND.equals(action)){ 16 if ("text/plain".equals(type)){ 17 String info = intent.getStringExtra(Intent.EXTRA_TEXT); 18 content.setText(info); 19 } 20 } 21 } 22 }
---恢复内容结束---
发送简单文本:
- 意图允许用户快速而方便地共享信息
- Intent sendIntent = new Intent();
- sendIntent.setAction(Intent.ACTION_SEND);
- sendIntent.putExtra(Intent.EXTRA_TEXT,"This is my text to send");
- sendIntent.setType("text/plain");
- startActivity(sendIntent);
- 指定选择器当有多个时可以指定:startActivity(Intent.createChooser(sendIntent,getResources().getText(R.sting.send_to)));
- 点击按钮分享:
1 public void share_text(View view) { 2 Intent text_it = new Intent(); 3 /*标准发送,qq,微信,等都以匹配到*/ 4 text_it.setAction(Intent.ACTION_SEND); 5 text_it.setType("text/plain"); 6 text_it.putExtra(Intent.EXTRA_TEXT,"你要发送的内容"); 7 // startActivity(text_it); 8 startActivity(Intent.createChooser(text_it,"你要发送的标题")); 9 }
发送二进制内容:
1 /*二进制:分享图片为例*/ 2 public void share_img(View view) { 3 Uri uri = Uri.parse("/sdcard/1.jpg"); 4 Intent img_it = new Intent(); 5 /*标准发送,qq,微信,等都以匹配到*/ 6 img_it.setAction(Intent.ACTION_SEND); 7 /* *:所有格式*/ 8 img_it.setType("image/*"); 9 img_it.putExtra(Intent.EXTRA_STREAM,uri); 10 // startActivity(text_it); 11 startActivity(Intent.createChooser(img_it,"你要分享的图片标题")); 12 }
发送多件内容:
1 /*分享多文件:分享多个图片为例*/ 2 public void share_imgs(View view) { 3 Uri uri1 = Uri.parse("/sdcard/1.jpg"); 4 Uri uri2 = Uri.parse("/sdcard/2.jpg"); 5 Uri uri3 = Uri.parse("/sdcard/3.jpg"); 6 ArrayList<Uri> list = new ArrayList<>(); 7 list.add(uri1); 8 list.add(uri2); 9 list.add(uri3); 10 Intent imgs_it = new Intent(); 11 imgs_it.setAction(Intent.ACTION_SEND_MULTIPLE); 12 /* *:所有格式*/ 13 imgs_it.setType("image/*"); 14 imgs_it.putParcelableArrayListExtra(Intent.EXTRA_STREAM,list); 15 // startActivity(text_it); 16 startActivity(Intent.createChooser(imgs_it,"你多个文件分享的图片标题")); 17 }
ShareActionProvider:
在onCreateOptionsMenu方法中:
1 @Override 2 public boolean onCreateOptionsMenu(Menu menu) { 3 getMenuInflater().inflate(R.menu.menu,menu); 4 MenuItem menuItem = menu.findItem(R.id.shared); 5 ShareActionProvider sap = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); 6 Intent intent = new Intent(); 7 intent.setAction(Intent.ACTION_SEND); 8 intent.setType("text/plain"); 9 intent.putExtra(Intent.EXTRA_TEXT,"您要分享的内容"); 10 sap.setShareIntent(intent); 11 return super.onCreateOptionsMenu(menu); 12 }
Menu中文件menu.xml:注意导入v7包:
1 <?xml version="1.0" encoding="utf-8"?> 2 <menu xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto"> 4 <item android:id="@+id/action_setting" 5 android:title="Setting" 6 android:orderInCategory="100" 7 app:showAsAction="never"/> 8 <item 9 android:id="@+id/shared" 10 android:title="分享" 11 android:orderInCategory="200" 12 app:showAsAction="ifRoom" 13 app:actionProviderClass="android.support.v7.widget.ShareActionProvider" 14 /> 15 </menu>
接收其他应用发出的内容:
在组件中清单文件的设置:
1 <!--接收对象信息设置--> 2 <activity android:name=".ReceiverShareMainActivity"> 3 <intent-filter> 4 <action android:name="android.intent.action.SEND"/> 5 <category android:name="android.intent.category.DEFAULT"/> 6 <data android:mimeType="text/plain"/> 7 </intent-filter> 8 </activity>
组件实例:
1 public class ReceiverShareMainActivity extends AppCompatActivity { 2 private TextView content ; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_receiver_share_main); 7 content = (TextView) findViewById(R.id.content); 8 handlerContent(); 9 } 10 private void handlerContent() { 11 Intent intent = getIntent() ; 12 /*根据类型不同做出相应的操作*/ 13 String type = intent.getType(); 14 String action = intent.getAction(); 15 if (Intent.ACTION_SEND.equals(action)){ 16 if ("text/plain".equals(type)){ 17 String info = intent.getStringExtra(Intent.EXTRA_TEXT); 18 content.setText(info); 19 } 20 } 21 } 22 }