用Intent.ACTION_SEND进行分享
2016-01-29 02:48
369 查看
分享现在有许多集成的,如友盟,ShareSDK等,或者直接去下载哪个App的分享SDK来集成,这些分享都是会提示来自xxx(appName)。如果不需要,可以简单通过Intent.ACTION_SEND来实现分享功能
可以看到,仅只需要传送上下文和内容,一次分享动作就实现
分享纯文本
如上面,调用
分享图片
调用方式:
注意:这里有写SD卡动作,需要添加读写权限,6.0添加运行时判断
分享多张图片
调用方式:
分享接口
下载
分享
下面通过一段代码来看看分享的实现public static void share(Context context, String extraText) { Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT, extraText); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity( Intent.createChooser(shareIntent, context.getString(R.string.action_share))); }
可以看到,仅只需要传送上下文和内容,一次分享动作就实现
分享纯文本
如上面,调用
ShareUtil.share(this, "test share");就实现了分享文本
分享图片
public static void share(Context context, Uri uri) { Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); shareIntent.setType("image/*"); context.startActivity( Intent.createChooser(shareIntent, context.getString(R.string.action_share))); }
调用方式:
File file = BitmapUtil.Drawable2File(this, R.mipmap.ic_launcher, Environment.getExternalStorageDirectory() + "/test.png"); uri = BitmapUtil.File2Uri(file); ShareUtil.share(this, uri);
注意:这里有写SD卡动作,需要添加读写权限,6.0添加运行时判断
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
分享多张图片
public static void share(Context context, ArrayList<Uri> uris) { Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); shareIntent.setType("image/*"); context.startActivity( Intent.createChooser(shareIntent, context.getString(R.string.action_share))); }
调用方式:
ShareUtil.share(this, uris);
接收分享
从分享动作可以看出,只是发了一个Intent出来,所以其它App也是没有硬性要求接收你的分享数据 – 不写接口咯,但我们可以自己写个接收分享数据接口出来,也是几行代码。AndroidManifest.xml添加
intent-filter
<activity android:name=".ShareReceiveActivity"> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="image/*"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND_MULTIPLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="image/*"/> </intent-filter> </activity>
分享接口
public class ShareReceiveActivity extends AppCompatActivity { @Bind(R.id.type) TextView type; @Bind(R.id.text) TextView text; @Bind(R.id.image) ImageView image; @Bind(R.id.image_multiple) ImageView imageMultiple; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_share_receive); ButterKnife.bind(this); // Get intent, action and MIME type Intent intent = getIntent(); String action = intent.getAction(); String type = intent.getType(); if (Intent.ACTION_SEND.equals(action) && type != null) { if ("text/plain".equals(type)) { handleSendText(intent); // Handle text being sent } else if (type.startsWith("image/")) { handleSendImage(intent); // Handle single image being sent } } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { if (type.startsWith("image/")) { handleSendMultipleImages(intent); // Handle multiple images being sent } } else { // Handle other intents, such as being started from the home screen } } void handleSendText(Intent intent) { String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); if (sharedText != null) { // Update UI to reflect text being shared type.setText("type:text"); text.setText(sharedText); } } void handleSendImage(Intent intent) { Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); if (imageUri != null) { // Update UI to reflect image being shared type.setText("type:image"); image.setImageURI(imageUri); } } void handleSendMultipleImages(Intent intent) { ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); if (imageUris != null) { // Update UI to reflect multiple images being shared type.setText("type:multiple"); if (imageUris.size() == 2) { image.setImageURI(imageUris.get(0)); imageMultiple.setImageURI(imageUris.get(1)); } } } }
下载
相关文章推荐
- VC6.0下PHP扩展开发详细教程
- c语言:存款利息计算,1000元,存5年,求五种方案存款五年后的本息和
- cmake cannot find eigen3 in ubuntu
- readbook:自己设计mvc框架,java类似struts2的实现
- HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法
- Windows下编译安装php扩展eAccelerator教程
- mac上设置android的sdk-ndk环境变量
- 前端模块管理器
- TC SRM 680(div 2) 1000pts
- DirectX 配置 vs2013 Win10 64bit
- uwsgi 热启动
- 什么叫匿名函数
- CUDA/OpenCL 学习资料
- c语言:我国国民生产总值的年增长率为10%,10年后国民生产总值与现在相比增长多少
- HDOJ 2082 找单词(母函数模板题)
- CF-Contest339-614
- 51nod(1081 字段求和)(dp、树状数组、线段树求法)
- 使用两台Kinect2.0和ipi studio进行动作捕捉
- POJ3252-RoundNumbers-排列组合
- UIScrollview 与 Autolayout 的那点事