Android实现原生分享功能

本文实例为大家分享了Android实现分享功能的具体代码,供大家参考,具体内容如下

因为公司的需求,最近一直在做分享这一块的功能。大概有这样几种思路:

1.使用Intent调用andoird原生的分享功能;

2.使用第三方的sdk,比如ShareSdk或者友盟;

3.去对应的平台下载jar包,参考官方设计文档写出自己的分享demo,但这种一般也比较复杂,尤其搞不懂qq和微信一家公司的,为什么微信那么麻烦。

不废话了,直接上代码:

一. 新建ShareUtil.java类

import java.io.File; 

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;
public class ShareUtil {
  private Context context; 

  public ShareUtil(Context context) {
    this.context = context;
  } 

  public static final String WEIXIN_PACKAGE_NAME = "";
  public static final String QQ_PACKAGE_NAME = "";
// public static final String ; 

  /**
   * 分享文字
   * @param packageName
   * @param content
   * @param title
   * @param subject
   */
  public void shareText(String packageName,String className,String content,String title,String subject){
      Intent intent =new Intent();
      intent.setAction(Intent.ACTION_SEND);
      intent.setType("text/plain");
  //   if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){
  //
  //   }else {
  //     if(null != packageName && !TextUtils.isEmpty(packageName)){
  //       intent.setPackage(packageName);
  //     }
  //   }
      if(stringCheck(className) && stringCheck(packageName)){
        ComponentName componentName = new ComponentName(packageName, className);
        intent.setComponent(componentName);
      }else if(stringCheck(packageName)){
        intent.setPackage(packageName);
      } 

      intent.putExtra(Intent.EXTRA_TEXT, content);
      if(null != title && !TextUtils.isEmpty(title)){
        intent.putExtra(Intent.EXTRA_TITLE, title);
      }
      if(null != subject && !TextUtils.isEmpty(subject)){
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
      }
      intent.putExtra(Intent.EXTRA_TITLE, title);
      Intent chooserIntent = Intent.createChooser(intent, "分享到:");
      context.startActivity(chooserIntent);
    } 

  /**
   * 分享网页
   */
  public void shareUrl(String packageName,String className,String content,String title,String subject){
    Intent intent =new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("text/plain");
//   if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){
//
//   }else {
//     if(null != packageName && !TextUtils.isEmpty(packageName)){
//       intent.setPackage(packageName);
//     }
//   }
    if(stringCheck(className) && stringCheck(packageName)){
      ComponentName componentName = new ComponentName(packageName, className);
      intent.setComponent(componentName);
    }else if(stringCheck(packageName)){
      intent.setPackage(packageName);
    } 

    intent.putExtra(Intent.EXTRA_TEXT, content);
    if(null != title && !TextUtils.isEmpty(title)){
      intent.putExtra(Intent.EXTRA_TITLE, title);
    }
    if(null != subject && !TextUtils.isEmpty(subject)){
      intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    }
    intent.putExtra(Intent.EXTRA_TITLE, title);
    Intent chooserIntent = Intent.createChooser(intent, "分享到:");
    context.startActivity(chooserIntent);
  } 

  /**
   * 分享图片
   */
  public void shareImg(String packageName,String className,File file){
    if(file.exists()){
      Uri uri = Uri.fromFile(file);
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_SEND);
      intent.setType("image/*");
      if(stringCheck(packageName) && stringCheck(className)){
        intent.setComponent(new ComponentName(packageName, className));
      }else if (stringCheck(packageName)) {
        intent.setPackage(packageName);
      }
      intent.putExtra(Intent.EXTRA_STREAM, uri);
      Intent chooserIntent = Intent.createChooser(intent, "分享到:");
      context.startActivity(chooserIntent);
    }else {
      Toast.makeText(context, "文件不存在", 1000).show();
    }
  } 

  /**
   * 分享音乐
   */
  public void shareAudio(String packageName,String className,File file){
    if(file.exists()){
      Uri uri = Uri.fromFile(file);
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_SEND);
      intent.setType("audio/*");
      if(stringCheck(packageName) && stringCheck(className)){
        intent.setComponent(new ComponentName(packageName, className));
      }else if (stringCheck(packageName)) {
        intent.setPackage(packageName);
      }
      intent.putExtra(Intent.EXTRA_STREAM, uri);
      Intent chooserIntent = Intent.createChooser(intent, "分享到:");
      context.startActivity(chooserIntent);
    }else {
      Toast.makeText(context, "文件不存在", 1000).show();
    }
  } 

  /**
   * 分享视频
   */
  public void shareVideo(String packageName,String className,File file){
    setIntent("video/*", packageName, className, file);
  } 

  public void setIntent(String type,String packageName,String className,File file){
    if(file.exists()){
      Uri uri = Uri.fromFile(file);
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_SEND);
      intent.setType(type);
      if(stringCheck(packageName) && stringCheck(className)){
        intent.setComponent(new ComponentName(packageName, className));
      }else if (stringCheck(packageName)) {
        intent.setPackage(packageName);
      }
      intent.putExtra(Intent.EXTRA_STREAM, uri);
      Intent chooserIntent = Intent.createChooser(intent, "分享到:");
      context.startActivity(chooserIntent);
    }else {
      Toast.makeText(context, "文件不存在", 1000).show();
    }
  } 

  /**
   * 分享多张图片和文字至朋友圈
   * @param title
   * @param packageName
   * @param className
   * @param file 图片文件
   */
  public void shareImgToWXCircle(String title,String packageName,String className, File file){
    if(file.exists()){
      Uri uri = Uri.fromFile(file);
      Intent intent = new Intent();
      ComponentName comp = new ComponentName(packageName, className);
      intent.setComponent(comp);
      intent.setAction(Intent.ACTION_SEND);
      intent.setType("image/*");
      intent.putExtra(Intent.EXTRA_STREAM, uri);
      intent.putExtra("Kdescription", title);
      context.startActivity(intent);
    }else{
      Toast.makeText(context, "文件不存在", Toast.LENGTH_LONG).show();
    } 

  }
  /**
   * 是否安装分享app
   * @param packageName
   */
  public boolean checkInstall(String packageName){
    try {
      context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
      return true;
    } catch (NameNotFoundException e) {
      e.printStackTrace();
      Toast.makeText(context, "请先安装应用app", 1500).show();
      return false;
    }
  } 

  /**
   * 跳转官方安装网址
   */
  public void toInstallWebView(String url){
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    context.startActivity(intent);
  } 

  public static boolean stringCheck(String str){
    if(null != str && !TextUtils.isEmpty(str)){
      return true;
    }else {
      return false;
    }
  }
}

二. MainActivity.java类

import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; 

public class MainActivity extends Activity implements OnClickListener {
  Button btnQQ;
  Button btnWX;
  Button btnMore;
  Button btnWxFriendText;
  Button btnQQFriendText;
  Button btnWxFriendImg;
  Button btnQQFriendImg;
  Button btnWxFriendAudio;
  Button btnQQFriendAduio;
  Button btnWxFriendVideo;
  Button btnQQFriendVideo; 

  ShareUtil shareUtil;
  private Button btn_wxCircle_img; 

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnQQ = (Button) findViewById(R.id.btn_qq);
    btnWX = (Button) findViewById(R.id.btn_wx);
    btnMore = (Button) findViewById(R.id.btn_more);
    btnWxFriendText = (Button) findViewById(R.id.btn_wxFriend);
    btnQQFriendText = (Button) findViewById(R.id.btn_qqFriend);
    btnWxFriendImg = (Button) findViewById(R.id.btn_wxFriend_img);
    btnQQFriendImg = (Button) findViewById(R.id.btn_qqFriend_img);
    btnWxFriendAudio = (Button) findViewById(R.id.btn_wxFriend_audio);
    btnQQFriendAduio = (Button) findViewById(R.id.btn_qqFriend_audio);
    btnWxFriendVideo = (Button) findViewById(R.id.btn_wxFriend_video);
    btnQQFriendVideo = (Button) findViewById(R.id.btn_qqFriend_video);
    btn_wxCircle_img = (Button) findViewById(R.id.btn_wxCircle_img); 

    btnQQ.setOnClickListener(this);
    btnWX.setOnClickListener(this);
    btnMore.setOnClickListener(this);
    btnWxFriendText.setOnClickListener(this);
    btnQQFriendText.setOnClickListener(this);
    btnWxFriendImg.setOnClickListener(this);
    btnQQFriendImg.setOnClickListener(this);
    btnWxFriendAudio.setOnClickListener(this);
    btnQQFriendAduio.setOnClickListener(this);
    btnWxFriendVideo.setOnClickListener(this);
    btnQQFriendVideo.setOnClickListener(this);
    btn_wxCircle_img.setOnClickListener(this); 

    shareUtil = new ShareUtil(this);
  } 

  @Override
  public void onClick(View v) {
    String testImgPath = "/storage/emulated/legacy/display-client/picture/my.png"; 

    String testImagePath = Environment.getExternalStorageDirectory()
        + "/img.jpg"; 

    String testAudioPath = Environment.getExternalStorageDirectory()
        + "/audio.mp3";
    String testVideoPath = Environment.getExternalStorageDirectory()
        + "/video.mp4"; 

    File file = new File(testImgPath);
    File fileImage = new File(testImagePath);
    File fileAudio = new File(testAudioPath);
    File fileVideo = new File(testVideoPath);
    switch (v.getId()) {
    // qq&文字
    case R.id.btn_qq:
      shareUtil.shareText("com.tencent.mobileqq", null, "这是一条分享信息",
          "分享标题", "分享主题");
      break;
    // 微信&文字
    case R.id.btn_wx:
      shareUtil.shareText("com.tencent.mm", null, "这是一条分享信息", "分享标题",
          "分享主题");
      break;
    // 所有&文字
    case R.id.btn_more:
      shareUtil.shareText(null, null, "这是一条分享信息", "分享标题", "分享主题");
      break;
    // 微信朋友&文字
    case R.id.btn_wxFriend:
      if (shareUtil.checkInstall("com.tencent.mm")) {
        shareUtil.shareText("com.tencent.mm",
            "com.tencent.mm.ui.tools.ShareImgUI",
            "http://www.aiipu.com/", "分享标题", "分享主题");
      } else {
        shareUtil.toInstallWebView("http://weixin.qq.com/download");
      }
      break;
    // qq朋友&文字
    case R.id.btn_qqFriend:
      if (shareUtil.checkInstall("com.tencent.mobileqq")) {
        shareUtil.shareText("com.tencent.mobileqq",
            "com.tencent.mobileqq.activity.JumpActivity",
            "http://www.aiipu.com/", "分享标题", "分享主题");
      } else {
        shareUtil.toInstallWebView("http://im.qq.com/mobileqq/");
      }
      break;
    // 微信朋友&图片
    case R.id.btn_wxFriend_img:
      shareUtil.shareImg("com.tencent.mm",
          "com.tencent.mm.ui.tools.ShareImgUI", fileImage);
      break;
    // qq朋友&图片
    case R.id.btn_qqFriend_img:
      shareUtil.shareImg("com.tencent.mobileqq",
          "com.tencent.mobileqq.activity.JumpActivity", fileImage);
      break;
    case R.id.btn_wxFriend_audio:
      shareUtil.shareAudio("com.tencent.mm",
          "com.tencent.mm.ui.tools.ShareImgUI", fileAudio);
      break;
    case R.id.btn_qqFriend_audio:
      shareUtil.shareAudio("com.tencent.mobileqq",
          "com.tencent.mobileqq.activity.JumpActivity", fileAudio);
      break;
    case R.id.btn_wxFriend_video:
      shareUtil.shareVideo("com.tencent.mm",
          "com.tencent.mm.ui.tools.ShareImgUI", fileVideo);
      break;
    case R.id.btn_qqFriend_video:
      shareUtil.shareVideo("com.tencent.mobileqq",
          "com.tencent.mobileqq.activity.JumpActivity", fileVideo);
      break;
    case R.id.btn_wxCircle_img:
      shareUtil.shareImgToWXCircle("狗狗图片", "com.tencent.mm",
          "com.tencent.mm.ui.tools.ShareToTimeLineUI", fileImage);
      break;
    }
  }
}

三.布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.ai.ipu.share_inent.MainActivity" > 

  <Button
    android:id="@+id/btn_qq"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="qq"/> 

  <Button
    android:id="@+id/btn_wx"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="wx"
    android:layout_below="@+id/btn_qq"/> 

  <Button
    android:id="@+id/btn_more"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="more"
    android:layout_below="@+id/btn_wx"/> 

  <Button
    android:id="@+id/btn_wxFriend"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_more"
    android:text="wxFriendText"/> 

  <Button
    android:id="@+id/btn_qqFriend"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_wxFriend"
    android:text="qqFriendText" /> 

  <Button
    android:id="@+id/btn_wxFriend_img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_more"
    android:layout_toRightOf="@+id/btn_wxFriend"
    android:text="wxFriendImg" /> 

  <Button
    android:id="@+id/btn_qqFriend_img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btn_qqFriend"
    android:layout_below="@+id/btn_wxFriend"
    android:text="qqFriendImg" /> 

  <Button
    android:id="@+id/btn_wxFriend_audio"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_more"
    android:layout_toRightOf="@+id/btn_wxFriend_img"
    android:text="wxFriendAudio" /> 

  <Button
    android:id="@+id/btn_qqFriend_audio"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btn_qqFriend_img"
    android:layout_below="@+id/btn_wxFriend"
    android:text="qqFriendAudio" /> 

  <Button
    android:id="@+id/btn_wxFriend_video"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_qqFriend"
    android:text="wxFriendVideo" /> 

  <Button
    android:id="@+id/btn_qqFriend_video"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_wxFriend_video"
    android:text="qqFriendVideo" /> 

  <Button
    android:id="@+id/btn_wxCircle_img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_wxFriend_video"
    android:layout_toRightOf="@+id/btn_wxFriend_img"
    android:text="wxCircleImg" /> 

</RelativeLayout>

其中微信的分享只能分享文字和图片,不能单独分享图片或者文字。

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

(0)

相关推荐

  • Android自带API实现分享功能

    前言 在做项目的过程中需要实现文字和图片的分享,有两种方式: 1. 使用android sdk中自带的Intent.ACTION_SEND实现分享. 2. 使用shareSDK.友盟等第三方的服务. 鉴于使用的方便,此次只介绍使用Android sdk中自带的方式来实现分享的功能. 分享文字 /** * 分享文字内容 * * @param dlgTitle * 分享对话框标题 * @param subject * 主题 * @param content * 分享内容(文字) */ private

  • Android使用ShareSDK实现应用分享的功能

    简介 今天给大家带来的是使用ShareSDK实现应用分享的功能.下面我们先看下效果图. 效果图 步骤 1. 获取AppKey 访问mob官网http://mob.com注册账号–>点击头像进入"后台中心"–>选择shareSDK–>添加应用–>获取AppKey. 点击添加后,就可以生成项目对应的AppKey. 2. SDK下载 访问mob官网http://mob.com下载最新的sdk. 然后点击一键下载就可以下载最新的sdk. 3. sdk目录结构 上面这张图

  • Android系统自带分享图片功能

    简介 记录一个利用系统分享功能进行图片分享的工具类(代码是用Kotlin写的,都是比较简单的语法,部分可能需要自定义的地方都已经标出).调用方式比较简单: Util.startShareImage(this) //this为当前的Activity实例 权限 记得添加文件操作权限, 另外需要注意6.0版本以上的权限管理 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <

  • Android使用WebView实现截图分享功能

    在APP项目的开发过程中,经常会用到分享图片的功能,有时候还需要根据当前用户信息获取指定的分享图片,比如要求在用户分享图中显示用户名.Uid.用户头像等信息.想到的实现方法主要有两点: 1.通过android SDK自带的Canvas方法进行绘制. 2.通过webView实现客户端与H5交互,然后将H5界面做截图处理. 本文主要介绍第二种方式的实现过程,第一种方式的实现方法,后续有时间会在博客中做说明,下面开始本文内容. 首先确定我们要实现的逻辑: 1.客户端与H5的交互,客户端将用户信息(用户

  • android7.0实现分享图片到朋友圈功能

    本文实例为大家分享了android实现分享图片到朋友圈功能的具体代码,供大家参考,具体内容如下 在Android7.0中,系统对scheme为file://的uri进行了限制,所以通过这种uri来进行分享的一些接口就不能用了,比如使用代码来调用分享朋友圈的接口. 此时就得使用其他的URI scheme来代替 file://,比如MediaStore的 content://.直接上代码: private static boolean checkInstallation(Context contex

  • Android实现原生分享功能

    本文实例为大家分享了Android实现分享功能的具体代码,供大家参考,具体内容如下 因为公司的需求,最近一直在做分享这一块的功能.大概有这样几种思路: 1.使用Intent调用andoird原生的分享功能: 2.使用第三方的sdk,比如ShareSdk或者友盟: 3.去对应的平台下载jar包,参考官方设计文档写出自己的分享demo,但这种一般也比较复杂,尤其搞不懂qq和微信一家公司的,为什么微信那么麻烦. 不废话了,直接上代码: 一. 新建ShareUtil.java类 import java.

  • Android截屏分享功能

    最近项目需要实现Android截屏分享功能,包括Android截屏获取图片.将图片保存到本地.通知系统相册更新.通过微信.QQ.微博分享截屏图片,本篇文章作为总结回顾.     一.Android截屏获取图片 通过对view进行绘制,得到bitmap,可以对Activity.Fragment进行绘制,也可以对其他的View进行绘制.    1 Activity截图(带空白的状态栏) public Bitmap shotScreen(Activity activity) { View view =

  • Android 微信图片分享功能

    我们都知道,通过 微信官方 分享sdk 支持图片分享,而且有多种方式.官方链接 可直接查看,不再赘述. 本文要解决的问题是,分享本地带二维码的图片给微信好友和朋友圈.朋友圈图片能够实现长按识别,给微信好友对话框的图片 在 iOS 可以正常识别,但是 Android 端却不能识别 ,为什么? 以下引用网友的回答: 经过分析和功能对比,android wechat app 中有两种图片浏览方式,图片预览,和图片本地发送后的打开查看(这个有识别动作)页面.预览图片功能中,不包含长按手势的识别功能,仔细

  • Android实现截图和分享功能的代码

    先给大家展示下效果图吧 直接上代码: xml的布局: <Button android:id="@+id/btn_jp" android:layout_marginTop="10dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:tex

  • Android仿QQ空间动态界面分享功能

    先看看效果: 用极少的代码实现了 动态详情 及 二级评论 的 数据获取与处理 和 UI显示与交互,并且高解耦.高复用.高灵活. 动态列表界面MomentListFragment支持 下拉刷新与上拉加载 和 模糊搜索,反复快速滑动仍然非常流畅. 缓存机制使得数据可在启动界面后瞬间加载完成. 动态详情界面MomentActivity支持 (取消)点赞.(删除)评论.点击姓名跳到个人详情 等. 只有1张图片时图片放大显示,超过1张则按九宫格显示. 用到的CommentContainerView和Mom

  • Android编程实现调用系统分享功能示例

    本文实例讲述了Android编程实现调用系统分享功能.分享给大家供大家参考,具体如下: /** * 调用系统的分享功能 * Created by admin on 15-4-13. */ public class ShareActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV

  • Android实现分享功能

    Android应用中能很方便的完成这些功能,很多的应用中都有"分享"功能?如何分享呢?下面给大家说说看. 最近有人问到Android分享功能用那个比较好,使用Android自带的Intent来进行分享还是借助第三方呢,直接上代码: 一.使用Intent直接和第三方应用进行通信: /** * 分享功能 * * @param context * 上下文 * @param activityTitle * Activity的名字 * @param msgTitle * 消息标题 * @para

  • Android开发中应用程序分享功能实例

    本文实例讲述了Android开发中应用程序分享功能.分享给大家供大家参考,具体如下: Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); //设置类型 shareIntent.setType("text/plain"); //设置分享的主题 shareIntent.putExtra("android.intent.extra.SUBJECT", "分享&

  • Android编程之微信SDK分享功能过程步骤详细分析

    本文实例讲述了Android编程之微信SDK分享功能过程步骤详细分析.分享给大家供大家参考,具体如下: 之前已经分析过怎么用官方的demo分享微信信息了,在这里我就不再多说,其中关于在自己应用分享说得很简单,本文作者也是经过一番折腾才弄成功,为了以后让大家都少走弯路,决定在这里从头到尾介绍怎么在自己的应用中分享功能 注意:顺序不能乱!! 1.建立自己的应用 TestShareWX (1)应用包名是com.freeson.test,然后建立一个测试Activity,名字为TestActivity,

随机推荐