android intent 分发,Android分享操作

现在用的比较多的都是三方的分享,其实安卓自带的就有简单的分享。

在构建Intent时,可以指定这个Intent需要触发的actions。比如ACTION_SEND,该action表明该intent用于从一个activity发送数据到另外一个activity的,甚至可以跨进程之间的数据发送。系统会自动识别出能够兼容接受的这些数据的activity。如果这些选择有多个,则把这些activity显示给用户进行选择;如果只有一个,则立即启动该Activity。

分享数据

分享简单的数据

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);

在不同的程序之间使用intent收发数据是在社交分享内容时最常用的方法。

若有多个匹配的程序,则系统会把他们都给筛选出来,并呈现Dialog给用户进行选择。

如果设备上安装有某个能够匹配ACTION_SEND且MIME类型为text/plain的程序,则Android系统会立即执行它。

不过为intent调用了Intent.createChooser(),那么Android总是会显示可供选择:

startActivity(Intent.createChooser(sendIntent,getResources().getText(R.string.send_to)));

看下显示效果:

f790833e669d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

f790833e669d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

f790833e669d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

分享图片

如果想要分享图片不是文字的话,首先得将setType设置成“image/jpeg”,如果不确定图片类型直接使用"image/*,同时数据需要结合设置特定的MIME类型,EXTRA_STREAM里面放置数据的URI。

Intent sendIntent = new Intent();

sendIntent.setAction(Intent.ACTION_SEND);

sendIntent.putExtra(Intent.EXTRA_STREAM, mCroppedImageFile.getPath());

sendIntent.setType("image/");

startActivity(Intent.createChooser(sendIntent, "分享"));

f790833e669d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

f790833e669d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

f790833e669d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

分享多种类型数据

如果分享3张JPEG的图片,那么MIME类型仍然是image/jpeg。如果是不同图片格式的话,应该是用image/来匹配那些可以接收任何图片类型的activity。如果需要分享多种不同类型的数据,可以使用/*来表示MIME。

一次分享多张图片

ArrayList imageUris = new ArrayList<>();

imageUris.add(Uri.fromFile(mCroppedImageFile));

imageUris.add(Uri.fromFile(mCroppedImageFile));

Intent sendIntent = new Intent();

sendIntent.setAction(Intent.ACTION_SEND_MULTIPLE);

sendIntent.putExtra(Intent.EXTRA_STREAM, imageUris);

sendIntent.setType("image/*");

startActivity(Intent.createChooser(sendIntent, "分享"));

可以看到同时分发了2张图片:

f790833e669d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

接收从其他App传送来的数据

让自己的app可以接受数据

Intent filters告诉Android系统一个程序愿意接受的数据类型。

android:name=".MainActivity"

android:theme="@style/CustomActionBarTheme">

上面将当前app的MainActivity可以接受文字。然后我们用另外一个app来分享文字:

f790833e669d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

上图中的TraningApp就是我自己定义能接受文字分享的app

处理接受到的数据

为了处理从Intent带来的数据,可以通过调用getIntent()方法来获取到Intent对象。拿到这个对象后,我们可以对其中面的数据进行判断,从而决定下一步行为。

Intent intent = getIntent();

String action = intent.getAction();

String type = intent.getType();

if (Intent.ACTION_SEND.equals(action)){

if ("text/plain".equals(type)){

Log.e(TAG, "ACTION_SEND:"+intent.getStringExtra(Intent.EXTRA_TEXT) );

}

}

然后发送消息:

Intent sendIntent = new Intent();

sendIntent.setAction(Intent.ACTION_SEND);

sendIntent.putExtra(Intent.EXTRA_TEXT, "测试文字");

sendIntent.setType("text/plain");

startActivity(Intent.createChooser(sendIntent, "分享"));

查看log日志:

07-26 12:32:51.315 9746-9746/com.example.frc.trainingapp E/MainActivity: ACTION_SEND:测试文字

同样我们可以添加图片的处理:

if (Intent.ACTION_SEND.equals(action)) {

if ("text/plain".equals(type)) {

Log.e(TAG, "ACTION_SEND:" + intent.getStringExtra(Intent.EXTRA_TEXT));

} else if ("image/*".equals(type)) {

Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);

Log.e(TAG, "ACTION_SEND_URI:"+uri.toString());

}

}

查看Log日志:

07-26 14:09:37.135 30944-30944/com.yanxiu.yxsanke_android E/SK::: /storage/emulated/0/YXSanKe_Android/res/nnnn.jpg

note:需要注意的是处理发送过来的数据可能会是耗时操作,建议不要在UI线程进行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值