发布于 2017-10-06 01:53:24 | 169 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


本篇文章主要介绍了Android调用系统自带的分享功能实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

实现分享功能的几个办法

1.调用系统的分享功能

2.通过第三方SDK,如ShareSDK,友盟等

3.自行使用各自平台的SDK,比如QQ,微信,微博各自的SDK

这里就记录下第一种办法。

分享文本信息


    Intent textIntent = new Intent(Intent.ACTION_SEND);
    textIntent.setType("text/plain");
    textIntent.putExtra(Intent.EXTRA_TEXT, "这是一段分享的文字");
    startActivity(Intent.createChooser(textIntent, "分享"));

效果如下图:

分享单张图片


    String path = getResourcesUri(R.drawable.shu_1);
    Intent imageIntent = new Intent(Intent.ACTION_SEND);
    imageIntent.setType("image/jpeg");
    imageIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
    startActivity(Intent.createChooser(imageIntent, "分享"));

分享多个文件


    ArrayList<Uri> imageUris = new ArrayList<>();
    Uri uri1 = Uri.parse(getResourcesUri(R.drawable.dog));
    Uri uri2 = Uri.parse(getResourcesUri(R.drawable.shu_1));
    imageUris.add(uri1);
    imageUris.add(uri2);
    Intent mulIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    mulIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
    mulIntent.setType("image/jpeg");
    startActivity(Intent.createChooser(mulIntent,"多文件分享"));

上面几个例子的效果都是一样的,创建一个选择器,让用户自己选择分享到哪里。

这里有一点得注意,就是通过这种方法进行分享,Intent传递的数据的Type(就是setType()方法)一定要控制好,不然会出错。(至于为什么后面说)。

其中由于是分享的res中的图片,故转变为uri,方法在这:


 private String getResourcesUri(@DrawableRes int id) {
  Resources resources = getResources();
  String uriPath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
    resources.getResourcePackageName(id) + "/" +
    resources.getResourceTypeName(id) + "/" +
    resources.getResourceEntryName(id);
  Toast.makeText(this, "Uri:" + uriPath, Toast.LENGTH_SHORT).show();
  return uriPath;
 }

指定分享到微信


    Intent wechatIntent = new Intent(Intent.ACTION_SEND);
    wechatIntent.setPackage("com.tencent.mm");
    wechatIntent.setType("text/plain");
    wechatIntent.putExtra(Intent.EXTRA_TEXT, "分享到微信的内容");
    startActivity(wechatIntent);

效果如下:

指定分享到QQ


    Intent qqIntent = new Intent(Intent.ACTION_SEND);
    qqIntent.setPackage("com.tencent.mobileqq");
    qqIntent.setType("text/plain");
    qqIntent.putExtra(Intent.EXTRA_TEXT, "分享到微信的内容");
    startActivity(qqIntent);

效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务