Android原生分享功能的思考与实现

来源:互联网 发布:南昌市残疾淘宝培训 编辑:程序博客网 时间:2024/06/08 16:39

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

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

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

不废话了,直接上代码:


一. 新建ShareUtil.Java类

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">import java.io.File;  
  2.   
  3. import android.content.ComponentName;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.pm.PackageManager;  
  7. import android.content.pm.PackageManager.NameNotFoundException;  
  8. import android.net.Uri;  
  9. import android.text.TextUtils;  
  10. import android.widget.Toast;  
  11. public class ShareUtil {  
  12.     private Context context;  
  13.       
  14.     public ShareUtil(Context context) {  
  15.         this.context = context;  
  16.     }  
  17.       
  18.     public static final String WEIXIN_PACKAGE_NAME = "";  
  19.     public static final String QQ_PACKAGE_NAME = "";  
  20. //  public static final String ;  
  21.       
  22.       
  23.     /** 
  24.      * 分享文字 
  25.      * @param packageName 
  26.      * @param content 
  27.      * @param title 
  28.      * @param subject 
  29.      */  
  30.     public void shareText(String packageName,String className,String content,String title,String subject){  
  31.             Intent intent =new Intent();  
  32.             intent.setAction(Intent.ACTION_SEND);  
  33.             intent.setType("text/plain");  
  34.     //      if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){  
  35.     //            
  36.     //      }else {           
  37.     //          if(null != packageName && !TextUtils.isEmpty(packageName)){  
  38.     //              intent.setPackage(packageName);  
  39.     //          }  
  40.     //      }  
  41.             if(stringCheck(className) && stringCheck(packageName)){  
  42.                 ComponentName componentName = new ComponentName(packageName, className);  
  43.                 intent.setComponent(componentName);  
  44.             }else if(stringCheck(packageName)){  
  45.                 intent.setPackage(packageName);  
  46.             }  
  47.               
  48.             intent.putExtra(Intent.EXTRA_TEXT, content);  
  49.             if(null != title && !TextUtils.isEmpty(title)){           
  50.                 intent.putExtra(Intent.EXTRA_TITLE, title);  
  51.             }  
  52.             if(null != subject && !TextUtils.isEmpty(subject)){  
  53.                 intent.putExtra(Intent.EXTRA_SUBJECT, subject);  
  54.             }  
  55.             intent.putExtra(Intent.EXTRA_TITLE, title);  
  56.             Intent chooserIntent = Intent.createChooser(intent, "分享到:");  
  57.             context.startActivity(chooserIntent);  
  58.         }  
  59.       
  60.     /** 
  61.      * 分享网页 
  62.      */  
  63.     public void shareUrl(String packageName,String className,String content,String title,String subject){  
  64.         Intent intent =new Intent();  
  65.         intent.setAction(Intent.ACTION_SEND);  
  66.         intent.setType("text/plain");  
  67. //      if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){  
  68. //            
  69. //      }else {           
  70. //          if(null != packageName && !TextUtils.isEmpty(packageName)){  
  71. //              intent.setPackage(packageName);  
  72. //          }  
  73. //      }  
  74.         if(stringCheck(className) && stringCheck(packageName)){  
  75.             ComponentName componentName = new ComponentName(packageName, className);  
  76.             intent.setComponent(componentName);  
  77.         }else if(stringCheck(packageName)){  
  78.             intent.setPackage(packageName);  
  79.         }  
  80.           
  81.         intent.putExtra(Intent.EXTRA_TEXT, content);  
  82.         if(null != title && !TextUtils.isEmpty(title)){           
  83.             intent.putExtra(Intent.EXTRA_TITLE, title);  
  84.         }  
  85.         if(null != subject && !TextUtils.isEmpty(subject)){  
  86.             intent.putExtra(Intent.EXTRA_SUBJECT, subject);  
  87.         }  
  88.         intent.putExtra(Intent.EXTRA_TITLE, title);  
  89.         Intent chooserIntent = Intent.createChooser(intent, "分享到:");  
  90.         context.startActivity(chooserIntent);  
  91.     }  
  92.       
  93.     /** 
  94.      * 分享图片 
  95.      */  
  96.     public void shareImg(String packageName,String className,File file){  
  97.         if(file.exists()){  
  98.             Uri uri = Uri.fromFile(file);  
  99.             Intent intent = new Intent();  
  100.             intent.setAction(Intent.ACTION_SEND);  
  101.             intent.setType("image/*");  
  102.             if(stringCheck(packageName) && stringCheck(className)){  
  103.                 intent.setComponent(new ComponentName(packageName, className));  
  104.             }else if (stringCheck(packageName)) {  
  105.                 intent.setPackage(packageName);  
  106.             }  
  107.             intent.putExtra(Intent.EXTRA_STREAM, uri);  
  108.             Intent chooserIntent = Intent.createChooser(intent, "分享到:");  
  109.             context.startActivity(chooserIntent);  
  110.         }else {  
  111.             Toast.makeText(context, "文件不存在"1000).show();  
  112.         }  
  113.     }  
  114.       
  115.     /**  
  116.      * 分享音乐  
  117.      */  
  118.     public void shareAudio(String packageName,String className,File file){  
  119.         if(file.exists()){  
  120.             Uri uri = Uri.fromFile(file);  
  121.             Intent intent = new Intent();  
  122.             intent.setAction(Intent.ACTION_SEND);  
  123.             intent.setType("audio/*");  
  124.             if(stringCheck(packageName) && stringCheck(className)){  
  125.                 intent.setComponent(new ComponentName(packageName, className));  
  126.             }else if (stringCheck(packageName)) {  
  127.                 intent.setPackage(packageName);  
  128.             }  
  129.             intent.putExtra(Intent.EXTRA_STREAM, uri);  
  130.             Intent chooserIntent = Intent.createChooser(intent, "分享到:");  
  131.             context.startActivity(chooserIntent);  
  132.         }else {  
  133.             Toast.makeText(context, "文件不存在"1000).show();  
  134.         }  
  135.     }  
  136.       
  137.     /**  
  138.      * 分享视频  
  139.      */  
  140.     public void shareVideo(String packageName,String className,File file){  
  141.         setIntent("video/*", packageName, className, file);  
  142.     }  
  143.       
  144.     public void setIntent(String type,String packageName,String className,File file){  
  145.         if(file.exists()){  
  146.             Uri uri = Uri.fromFile(file);  
  147.             Intent intent = new Intent();  
  148.             intent.setAction(Intent.ACTION_SEND);  
  149.             intent.setType(type);  
  150.             if(stringCheck(packageName) && stringCheck(className)){  
  151.                 intent.setComponent(new ComponentName(packageName, className));  
  152.             }else if (stringCheck(packageName)) {  
  153.                 intent.setPackage(packageName);  
  154.             }  
  155.             intent.putExtra(Intent.EXTRA_STREAM, uri);  
  156.             Intent chooserIntent = Intent.createChooser(intent, "分享到:");  
  157.             context.startActivity(chooserIntent);  
  158.         }else {  
  159.             Toast.makeText(context, "文件不存在"1000).show();  
  160.         }  
  161.     }  
  162.        
  163.     /**  
  164.      * 分享多张图片和文字至朋友圈  
  165.      * @param title   
  166.      * @param packageName  
  167.      * @param className  
  168.      * @param file 图片文件  
  169.      */  
  170.     public void shareImgToWXCircle(String title,String packageName,String className, File file){  
  171.         if(file.exists()){  
  172.             Uri uri = Uri.fromFile(file);  
  173.             Intent intent = new Intent();  
  174.             ComponentName comp = new ComponentName(packageName, className);  
  175.             intent.setComponent(comp);  
  176.             intent.setAction(Intent.ACTION_SEND);  
  177.             intent.setType("image/*");  
  178.             intent.putExtra(Intent.EXTRA_STREAM, uri);  
  179.             intent.putExtra("Kdescription", title);  
  180.             context.startActivity(intent);  
  181.         }else{  
  182.             Toast.makeText(context, "文件不存在", Toast.LENGTH_LONG).show();  
  183.         }  
  184.           
  185.           
  186.     }  
  187.     /**  
  188.      * 是否安装分享app  
  189.      * @param packageName  
  190.      */  
  191.     public boolean checkInstall(String packageName){  
  192.         try {  
  193.             context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);  
  194.             return true;  
  195.         } catch (NameNotFoundException e) {  
  196.             e.printStackTrace();  
  197.             Toast.makeText(context, "请先安装应用app"1500).show();  
  198.             return false;  
  199.         }  
  200.     }  
  201.       
  202.     /** 
  203.      * 跳转官方安装网址 
  204.      */  
  205.     public void toInstallWebView(String url){  
  206.         Intent intent = new Intent();  
  207.         intent.setAction(Intent.ACTION_VIEW);  
  208.         intent.setData(Uri.parse(url));  
  209.         context.startActivity(intent);  
  210.     }  
  211.       
  212.     public static boolean stringCheck(String str){  
  213.         if(null != str && !TextUtils.isEmpty(str)){  
  214.             return true;  
  215.         }else {  
  216.             return false;  
  217.         }  
  218.     }  
  219. }</span>  




二. MainActivity.java类


[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">import java.io.File;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.os.Environment;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8.   
  9. public class MainActivity extends Activity implements OnClickListener {  
  10.     Button btnQQ;  
  11.     Button btnWX;  
  12.     Button btnMore;  
  13.     Button btnWxFriendText;  
  14.     Button btnQQFriendText;  
  15.     Button btnWxFriendImg;  
  16.     Button btnQQFriendImg;  
  17.     Button btnWxFriendAudio;  
  18.     Button btnQQFriendAduio;  
  19.     Button btnWxFriendVideo;  
  20.     Button btnQQFriendVideo;  
  21.   
  22.     ShareUtil shareUtil;  
  23.     private Button btn_wxCircle_img;  
  24.   
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.         btnQQ = (Button) findViewById(R.id.btn_qq);  
  30.         btnWX = (Button) findViewById(R.id.btn_wx);  
  31.         btnMore = (Button) findViewById(R.id.btn_more);  
  32.         btnWxFriendText = (Button) findViewById(R.id.btn_wxFriend);  
  33.         btnQQFriendText = (Button) findViewById(R.id.btn_qqFriend);  
  34.         btnWxFriendImg = (Button) findViewById(R.id.btn_wxFriend_img);  
  35.         btnQQFriendImg = (Button) findViewById(R.id.btn_qqFriend_img);  
  36.         btnWxFriendAudio = (Button) findViewById(R.id.btn_wxFriend_audio);  
  37.         btnQQFriendAduio = (Button) findViewById(R.id.btn_qqFriend_audio);  
  38.         btnWxFriendVideo = (Button) findViewById(R.id.btn_wxFriend_video);  
  39.         btnQQFriendVideo = (Button) findViewById(R.id.btn_qqFriend_video);  
  40.         btn_wxCircle_img = (Button) findViewById(R.id.btn_wxCircle_img);  
  41.   
  42.         btnQQ.setOnClickListener(this);  
  43.         btnWX.setOnClickListener(this);  
  44.         btnMore.setOnClickListener(this);  
  45.         btnWxFriendText.setOnClickListener(this);  
  46.         btnQQFriendText.setOnClickListener(this);  
  47.         btnWxFriendImg.setOnClickListener(this);  
  48.         btnQQFriendImg.setOnClickListener(this);  
  49.         btnWxFriendAudio.setOnClickListener(this);  
  50.         btnQQFriendAduio.setOnClickListener(this);  
  51.         btnWxFriendVideo.setOnClickListener(this);  
  52.         btnQQFriendVideo.setOnClickListener(this);  
  53.         btn_wxCircle_img.setOnClickListener(this);  
  54.   
  55.         shareUtil = new ShareUtil(this);  
  56.     }  
  57.   
  58.     @Override  
  59.     public void onClick(View v) {  
  60.         String testImgPath = "/storage/emulated/legacy/display-client/picture/my.png";  
  61.   
  62.         String testImagePath = Environment.getExternalStorageDirectory()  
  63.                 + "/img.jpg";  
  64.   
  65.         String testAudioPath = Environment.getExternalStorageDirectory()  
  66.                 + "/audio.mp3";  
  67.         String testVideoPath = Environment.getExternalStorageDirectory()  
  68.                 + "/video.mp4";  
  69.   
  70.         File file = new File(testImgPath);  
  71.         File fileImage = new File(testImagePath);  
  72.         File fileAudio = new File(testAudioPath);  
  73.         File fileVideo = new File(testVideoPath);  
  74.         switch (v.getId()) {  
  75.         // qq&文字  
  76.         case R.id.btn_qq:  
  77.             shareUtil.shareText("com.tencent.mobileqq"null"这是一条分享信息",  
  78.                     "分享标题""分享主题");  
  79.             break;  
  80.         // 微信&文字  
  81.         case R.id.btn_wx:  
  82.             shareUtil.shareText("com.tencent.mm"null"这是一条分享信息""分享标题",  
  83.                     "分享主题");  
  84.             break;  
  85.         // 所有&文字  
  86.         case R.id.btn_more:  
  87.             shareUtil.shareText(nullnull"这是一条分享信息""分享标题""分享主题");  
  88.             break;  
  89.         // 微信朋友&文字  
  90.         case R.id.btn_wxFriend:  
  91.             if (shareUtil.checkInstall("com.tencent.mm")) {  
  92.                 shareUtil.shareText("com.tencent.mm",  
  93.                         "com.tencent.mm.ui.tools.ShareImgUI",  
  94.                         "http://www.aiipu.com/""分享标题""分享主题");  
  95.             } else {  
  96.                 shareUtil.toInstallWebView("http://weixin.qq.com/download");  
  97.             }  
  98.             break;  
  99.         // qq朋友&文字  
  100.         case R.id.btn_qqFriend:  
  101.             if (shareUtil.checkInstall("com.tencent.mobileqq")) {  
  102.                 shareUtil.shareText("com.tencent.mobileqq",  
  103.                         "com.tencent.mobileqq.activity.JumpActivity",  
  104.                         "http://www.aiipu.com/""分享标题""分享主题");  
  105.             } else {  
  106.                 shareUtil.toInstallWebView("http://im.qq.com/mobileqq/");  
  107.             }  
  108.             break;  
  109.         // 微信朋友&图片  
  110.         case R.id.btn_wxFriend_img:  
  111.             shareUtil.shareImg("com.tencent.mm",  
  112.                     "com.tencent.mm.ui.tools.ShareImgUI", fileImage);  
  113.             break;  
  114.         // qq朋友&图片  
  115.         case R.id.btn_qqFriend_img:  
  116.             shareUtil.shareImg("com.tencent.mobileqq",  
  117.                     "com.tencent.mobileqq.activity.JumpActivity", fileImage);  
  118.             break;  
  119.         case R.id.btn_wxFriend_audio:  
  120.             shareUtil.shareAudio("com.tencent.mm",  
  121.                     "com.tencent.mm.ui.tools.ShareImgUI", fileAudio);  
  122.             break;  
  123.         case R.id.btn_qqFriend_audio:  
  124.             shareUtil.shareAudio("com.tencent.mobileqq",  
  125.                     "com.tencent.mobileqq.activity.JumpActivity", fileAudio);  
  126.             break;  
  127.         case R.id.btn_wxFriend_video:  
  128.             shareUtil.shareVideo("com.tencent.mm",  
  129.                     "com.tencent.mm.ui.tools.ShareImgUI", fileVideo);  
  130.             break;  
  131.         case R.id.btn_qqFriend_video:  
  132.             shareUtil.shareVideo("com.tencent.mobileqq",  
  133.                     "com.tencent.mobileqq.activity.JumpActivity", fileVideo);  
  134.             break;  
  135.         case R.id.btn_wxCircle_img:  
  136.             shareUtil.shareImgToWXCircle("狗狗图片""com.tencent.mm",  
  137.                     "com.tencent.mm.ui.tools.ShareToTimeLineUI", fileImage);  
  138.             break;  
  139.         }  
  140.     }  
  141. }</span>  


三.布局文件activity_main.xml

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context="com.ai.ipu.share_inent.MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/btn_qq"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="qq"/>  
  16.       
  17.     <Button  
  18.         android:id="@+id/btn_wx"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="wx"  
  22.         android:layout_below="@+id/btn_qq"/>  
  23.       
  24.     <Button  
  25.         android:id="@+id/btn_more"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="more"  
  29.         android:layout_below="@+id/btn_wx"/>  
  30.       
  31.     <Button  
  32.         android:id="@+id/btn_wxFriend"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:layout_below="@+id/btn_more"  
  36.         android:text="wxFriendText"/>  
  37.       
  38.     <Button  
  39.         android:id="@+id/btn_qqFriend"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:layout_below="@+id/btn_wxFriend"  
  43.         android:text="qqFriendText" />  
  44.       
  45.     <Button  
  46.         android:id="@+id/btn_wxFriend_img"  
  47.         android:layout_width="wrap_content"  
  48.         android:layout_height="wrap_content"  
  49.         android:layout_below="@+id/btn_more"  
  50.         android:layout_toRightOf="@+id/btn_wxFriend"  
  51.         android:text="wxFriendImg" />  
  52.       
  53.     <Button  
  54.         android:id="@+id/btn_qqFriend_img"  
  55.         android:layout_width="wrap_content"  
  56.         android:layout_height="wrap_content"  
  57.         android:layout_toRightOf="@+id/btn_qqFriend"  
  58.         android:layout_below="@+id/btn_wxFriend"  
  59.         android:text="qqFriendImg" />  
  60.       
  61.     <Button  
  62.         android:id="@+id/btn_wxFriend_audio"  
  63.         android:layout_width="wrap_content"  
  64.         android:layout_height="wrap_content"  
  65.         android:layout_below="@+id/btn_more"  
  66.         android:layout_toRightOf="@+id/btn_wxFriend_img"  
  67.         android:text="wxFriendAudio" />  
  68.       
  69.     <Button  
  70.         android:id="@+id/btn_qqFriend_audio"  
  71.         android:layout_width="wrap_content"  
  72.         android:layout_height="wrap_content"  
  73.         android:layout_toRightOf="@+id/btn_qqFriend_img"  
  74.         android:layout_below="@+id/btn_wxFriend"  
  75.         android:text="qqFriendAudio" />  
  76.       
  77.     <Button  
  78.         android:id="@+id/btn_wxFriend_video"  
  79.         android:layout_width="wrap_content"  
  80.         android:layout_height="wrap_content"  
  81.         android:layout_below="@+id/btn_qqFriend"  
  82.         android:text="wxFriendVideo" />  
  83.       
  84.     <Button  
  85.         android:id="@+id/btn_qqFriend_video"  
  86.         android:layout_width="wrap_content"  
  87.         android:layout_height="wrap_content"  
  88.         android:layout_below="@+id/btn_wxFriend_video"  
  89.         android:text="qqFriendVideo" />  
  90.       
  91.       
  92.     <Button  
  93.         android:id="@+id/btn_wxCircle_img"  
  94.         android:layout_width="wrap_content"  
  95.         android:layout_height="wrap_content"  
  96.         android:layout_below="@+id/btn_wxFriend_video"  
  97.         android:layout_toRightOf="@+id/btn_wxFriend_img"  
  98.         android:text="wxCircleImg" />  
  99.       
  100.       
  101.       
  102.       
  103. </RelativeLayout></span>  

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

0 0
原创粉丝点击