服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现原生分享功能

Android实现原生分享功能

2022-02-12 17:15瓶子的罐子 Android

这篇文章主要介绍了Android实现原生分享功能,只能分享文字和图片,不能单独分享图片或者文字,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

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

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

不废话了,直接上代码:

一. 新建ShareUtil.java类

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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类

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<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>

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

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

原文链接:https://blog.csdn.net/zh_ang_lei/article/details/52385678

延伸 · 阅读

精彩推荐
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7452021-03-11
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4642021-03-09
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4672021-01-03
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28