Android实现原生分享功能

作者:袖梨 2022-06-25

本文实例为大家分享了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

 
 
  

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

相关文章

精彩推荐