├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── styles.xml
│ │ │ │ ├── key_strings.xml
│ │ │ │ └── strings.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── xyzlf
│ │ │ │ └── share
│ │ │ │ ├── wxapi
│ │ │ │ └── WXEntryActivity.java
│ │ │ │ ├── ShareCallBack.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── xyzlf
│ │ │ └── share
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── xyzlf
│ │ └── share
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── shareLibrary
├── .gitignore
├── libs
│ ├── libammsdk-2.0.jar
│ └── open_sdk_r5756_lite.jar
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── drawable-xhdpi
│ │ │ │ ├── share_qq.png
│ │ │ │ ├── share_more.png
│ │ │ │ ├── share_qzone.png
│ │ │ │ ├── share_weibo.png
│ │ │ │ ├── share_default.png
│ │ │ │ ├── share_wechat.png
│ │ │ │ ├── share_wxcircle.png
│ │ │ │ └── weibosdk_dialog_bg.9.png
│ │ │ ├── drawable
│ │ │ │ ├── share_grid_item_pressed.xml
│ │ │ │ ├── share_weibo_btn_disabled.xml
│ │ │ │ ├── share_button_selector.xml
│ │ │ │ ├── share_btn_bg.xml
│ │ │ │ └── share_weibo_btn_selector.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ ├── anim
│ │ │ │ ├── activity_exit.xml
│ │ │ │ └── activity_show.xml
│ │ │ ├── values
│ │ │ │ ├── styles.xml
│ │ │ │ └── strings.xml
│ │ │ └── layout
│ │ │ │ ├── share_gridview_item.xml
│ │ │ │ └── share_activity_dialog.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── xyzlf
│ │ │ │ └── share
│ │ │ │ └── library
│ │ │ │ ├── interfaces
│ │ │ │ ├── IShareBase.java
│ │ │ │ ├── OnShareListener.java
│ │ │ │ └── ShareConstant.java
│ │ │ │ ├── channel
│ │ │ │ ├── ShareBase.java
│ │ │ │ ├── ShareByEmail.java
│ │ │ │ ├── ShareBySystem.java
│ │ │ │ ├── ShareBySms.java
│ │ │ │ ├── ShareByQZone.java
│ │ │ │ ├── ShareByQQ.java
│ │ │ │ ├── ShareByWeibo2.java
│ │ │ │ ├── ShareByWeixin.java
│ │ │ │ └── ShareByWeibo.java
│ │ │ │ ├── bean
│ │ │ │ ├── ChannelEntity.java
│ │ │ │ └── ShareEntity.java
│ │ │ │ ├── util
│ │ │ │ ├── ToastUtil.java
│ │ │ │ ├── ChannelUtil.java
│ │ │ │ ├── ManifestUtil.java
│ │ │ │ └── ShareUtil.java
│ │ │ │ ├── ShareBaseActivity.java
│ │ │ │ ├── request
│ │ │ │ ├── AbstractAsyncTask.java
│ │ │ │ └── BitmapAsyncTask.java
│ │ │ │ ├── AccessTokenKeeper.java
│ │ │ │ ├── ShareHandlerActivity.java
│ │ │ │ └── ShareDialogActivity.java
│ │ └── AndroidManifest.xml
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── xyzlf
│ │ └── com
│ │ └── share
│ │ └── library
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── share.png
├── 分享SDK使用说明文档.docx
├── 分享SDK使用说明文档.pdf
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── LICENSE
├── gradlew.bat
├── README.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/shareLibrary/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':shareLibrary'
2 |
--------------------------------------------------------------------------------
/share.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/share.png
--------------------------------------------------------------------------------
/分享SDK使用说明文档.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/分享SDK使用说明文档.docx
--------------------------------------------------------------------------------
/分享SDK使用说明文档.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/分享SDK使用说明文档.pdf
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/shareLibrary/libs/libammsdk-2.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/libs/libammsdk-2.0.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | /captures
8 | /gradle.properties
9 |
--------------------------------------------------------------------------------
/shareLibrary/libs/open_sdk_r5756_lite.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/libs/open_sdk_r5756_lite.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable-xhdpi/share_qq.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/src/main/res/drawable-xhdpi/share_qq.png
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable-xhdpi/share_more.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/src/main/res/drawable-xhdpi/share_more.png
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable-xhdpi/share_qzone.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/src/main/res/drawable-xhdpi/share_qzone.png
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable-xhdpi/share_weibo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/src/main/res/drawable-xhdpi/share_weibo.png
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable-xhdpi/share_default.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/src/main/res/drawable-xhdpi/share_default.png
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable-xhdpi/share_wechat.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/src/main/res/drawable-xhdpi/share_wechat.png
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable-xhdpi/share_wxcircle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/src/main/res/drawable-xhdpi/share_wxcircle.png
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable-xhdpi/weibosdk_dialog_bg.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xyzlf/ShareSDK/HEAD/shareLibrary/src/main/res/drawable-xhdpi/weibosdk_dialog_bg.9.png
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable/share_grid_item_pressed.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable/share_weibo_btn_disabled.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Mar 16 01:41:26 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable/share_button_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable/share_btn_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | -
5 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/test/java/com/xyzlf/share/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/xyzlf/share/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/interfaces/IShareBase.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.interfaces;
2 |
3 | import com.xyzlf.share.library.bean.ShareEntity;
4 |
5 | /**
6 | * Created by zhanglifeng
7 | */
8 | public interface IShareBase {
9 | /**
10 | * @param data {@link ShareEntity}
11 | * @param listener {@link OnShareListener}
12 | */
13 | void share(ShareEntity data, OnShareListener listener);
14 | }
15 |
--------------------------------------------------------------------------------
/app/src/main/res/values/key_strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
7 |
8 |
9 | your weixin key
10 |
11 |
12 | 222222
13 |
14 |
15 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/channel/ShareBase.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.channel;
2 |
3 | import android.content.Context;
4 |
5 | import com.xyzlf.share.library.interfaces.IShareBase;
6 |
7 |
8 | /**
9 | * Created by zhanglifeng
10 | */
11 | public abstract class ShareBase implements IShareBase {
12 |
13 | Context context;
14 |
15 | public ShareBase(Context context) {
16 | this.context = context;
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/shareLibrary/src/androidTest/java/com/xyzlf/com/share/library/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.com.share.library;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/anim/activity_exit.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
12 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/anim/activity_show.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ShareSDK
3 |
4 | 显示分享界面
5 |
6 | 调用QQ分享
7 | 调用微信分享
8 | 调用朋友圈分享
9 | 调用微博分享
10 | 调用QZone分享
11 |
12 | 分享大图
13 |
14 |
15 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/interfaces/OnShareListener.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.interfaces;
2 |
3 | /**
4 | * Created by zhanglifeng
5 | */
6 | public interface OnShareListener {
7 | /**
8 | * @param channel {@link ShareConstant#SHARE_CHANNEL_ALL} 渠道
9 | * @param status {@link ShareConstant#SHARE_STATUS_COMPLETE} {@link ShareConstant#SHARE_STATUS_FAILED}
10 | * {@link ShareConstant#SHARE_STATUS_CANCEL}
11 | */
12 | void onShare(int channel, int status);
13 | }
14 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/drawable/share_weibo_btn_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | -
7 |
8 |
9 |
10 |
11 |
12 |
13 | -
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/shareLibrary/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\j-hurong-ncf\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\j-hurong-ncf\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 | -ignorewarnings
19 | -keep class com.tencent.mm.sdk.** {
20 | *;
21 | }
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion '28.0.3'
6 |
7 | defaultConfig {
8 | applicationId "com.xyzlf.share"
9 | minSdkVersion 14
10 | targetSdkVersion 28
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | testImplementation 'junit:junit:4.12'
25 | implementation 'com.android.support:appcompat-v7:28.0.0'
26 |
27 | implementation ('com.xyzlf.share:sharesdk:0.0.10') {
28 | exclude group: 'com.android.support', module: 'appcompat-v7'
29 | }
30 |
31 | // compile project(':shareLibrary')
32 | }
33 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/bean/ChannelEntity.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.bean;
2 |
3 | /**
4 | * 渠道实体
5 | */
6 | public class ChannelEntity {
7 |
8 | private int channel;
9 | private int icon;
10 | private String name;
11 |
12 | public ChannelEntity(int channel, int icon, String name) {
13 | this.channel = channel;
14 | this.icon = icon;
15 | this.name = name;
16 | }
17 |
18 | public int getIcon() {
19 | return icon;
20 | }
21 |
22 | public void setIcon(int icon) {
23 | this.icon = icon;
24 | }
25 |
26 | public String getName() {
27 | return name;
28 | }
29 |
30 | public void setName(String name) {
31 | this.name = name;
32 | }
33 |
34 | public int getchannel() {
35 | return channel;
36 | }
37 |
38 | public void setchannel(int channel) {
39 | this.channel = channel;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/layout/share_gridview_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
18 |
19 |
26 |
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 xyzlf
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/layout/share_activity_dialog.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
19 |
20 |
25 |
26 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ShareLibrary
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 | QQ
28 | QQ空间
29 | 更多
30 |
31 |
32 |
33 | 图片保存失败
34 | SD卡未挂载
35 |
36 |
37 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
11 |
12 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/util/ToastUtil.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.util;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.StringRes;
5 | import android.text.TextUtils;
6 | import android.widget.Toast;
7 |
8 | /**
9 | * Created by zhanglifeng
10 | */
11 | public class ToastUtil {
12 |
13 | /**
14 | * 显示toast
15 | * @param context context
16 | * @param resId 资源id
17 | * @param isShort isShort
18 | */
19 | public static void showToast(Context context, @StringRes int resId, boolean isShort) {
20 | if (null == context) {
21 | return;
22 | }
23 | String msg = null;
24 | try {
25 | msg = context.getString(resId);
26 | } catch (Exception e) {
27 | }
28 | if (TextUtils.isEmpty(msg)) {
29 | return;
30 | }
31 | showToast(context, msg, isShort);
32 | }
33 |
34 | /**
35 | * 显示toast
36 | * @param context context
37 | * @param msg 信息
38 | * @param isShort isShort
39 | */
40 | public static void showToast(Context context, String msg, boolean isShort) {
41 | if (null == context) {
42 | return;
43 | }
44 | if (TextUtils.isEmpty(msg)) {
45 | return;
46 | }
47 | Toast.makeText(context, msg, isShort ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG).show();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/ShareBaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.support.v4.app.FragmentActivity;
7 |
8 | import com.xyzlf.share.library.interfaces.ShareConstant;
9 |
10 |
11 | public abstract class ShareBaseActivity extends FragmentActivity {
12 |
13 | protected int channel;
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | if (null == getIntent()) {
19 | finish();
20 | return;
21 | }
22 | initChannel();
23 | }
24 |
25 | protected void initChannel() {
26 | try {
27 | //Fuzz问题处理
28 | channel = getIntent().getIntExtra(ShareConstant.EXTRA_SHARE_CHANNEL, ShareConstant.SHARE_CHANNEL_ALL);
29 | } catch (Exception e) {}
30 | }
31 |
32 | /**
33 | * @param channel {@link ShareConstant#SHARE_CHANNEL_ALL}
34 | * @param status {@link ShareConstant#SHARE_STATUS_CANCEL}
35 | */
36 | public void finishWithResult(final int channel, final int status) {
37 | Intent intent = new Intent();
38 | intent.putExtra(ShareConstant.EXTRA_SHARE_CHANNEL, channel);
39 | intent.putExtra(ShareConstant.EXTRA_SHARE_STATUS, status);
40 | setResult(Activity.RESULT_OK, intent);
41 | finish();
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/request/AbstractAsyncTask.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.request;
2 |
3 | import android.os.AsyncTask;
4 |
5 | /**
6 | * Created by zhanglifeng on 16/6/17
7 | */
8 | public abstract class AbstractAsyncTask extends AsyncTask {
9 | private Exception exception;
10 | private T data;
11 |
12 | @Override
13 | protected T doInBackground(Void... voids) {
14 | try {
15 | data = doLoadData();
16 | exception = null;
17 | } catch (Exception e) {
18 | data = null;
19 | exception = e;
20 | }
21 | return getData();
22 | }
23 |
24 | protected abstract T doLoadData() throws Exception;
25 |
26 | public Exception getException() {
27 | return exception;
28 | }
29 |
30 | public T getData() {
31 | return data;
32 | }
33 |
34 | @Override
35 | protected void onPostExecute(T t) {
36 | try {
37 | if (exception == null) {
38 | onSuccess(t);
39 | } else {
40 | onException(exception);
41 | }
42 | } catch (Exception e) {
43 | e.printStackTrace();
44 | } finally {
45 | onFinally();
46 | }
47 |
48 | }
49 |
50 | public void onSuccess(T t) {
51 | }
52 |
53 | public void onFinally() {
54 | }
55 |
56 | public void onException(Exception exception) {
57 |
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xyzlf/share/wxapi/WXEntryActivity.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.wxapi;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 |
7 | import com.tencent.mm.sdk.modelbase.BaseReq;
8 | import com.tencent.mm.sdk.modelbase.BaseResp;
9 | import com.tencent.mm.sdk.openapi.IWXAPI;
10 | import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
11 | import com.tencent.mm.sdk.openapi.WXAPIFactory;
12 | import com.xyzlf.share.MainActivity;
13 | import com.xyzlf.share.library.interfaces.ShareConstant;
14 | import com.xyzlf.share.library.util.ManifestUtil;
15 |
16 | /**
17 | * 这个类是微信回调的类
18 | */
19 | public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
20 |
21 | private IWXAPI api;
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | api = WXAPIFactory.createWXAPI(this, ManifestUtil.getWeixinKey(this), false);
27 | api.handleIntent(getIntent(), this);
28 | }
29 |
30 | @Override
31 | protected void onNewIntent(Intent intent) {
32 | super.onNewIntent(intent);
33 | setIntent(intent);
34 | api.handleIntent(intent, this);
35 | }
36 |
37 | @Override
38 | public void onReq(BaseReq req) {
39 |
40 | }
41 |
42 | @Override
43 | public void onResp(BaseResp resp) {
44 | Intent intent = new Intent();
45 | intent.setAction(ShareConstant.ACTION_WEIXIN_CALLBACK);
46 | intent.putExtra(ShareConstant.EXTRA_WEIXIN_RESULT, resp.errCode);
47 | sendBroadcast(intent);
48 | finish();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
19 |
20 |
25 |
26 |
31 |
32 |
37 |
38 |
43 |
44 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/channel/ShareByEmail.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.channel;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.net.Uri;
6 | import android.text.TextUtils;
7 |
8 | import com.xyzlf.share.library.R;
9 | import com.xyzlf.share.library.bean.ShareEntity;
10 | import com.xyzlf.share.library.interfaces.OnShareListener;
11 | import com.xyzlf.share.library.interfaces.ShareConstant;
12 | import com.xyzlf.share.library.util.ShareUtil;
13 | import com.xyzlf.share.library.util.ToastUtil;
14 |
15 | /**
16 | * Created by zhanglifeng
17 | */
18 | public class ShareByEmail extends ShareBase {
19 |
20 | public ShareByEmail(Context context) {
21 | super(context);
22 | }
23 |
24 | @Override
25 | public void share(ShareEntity data, OnShareListener listener) {
26 | if (data == null || TextUtils.isEmpty(data.getContent())) {
27 | ToastUtil.showToast(context, R.string.share_empty_tip, true);
28 | return;
29 | }
30 | Intent email = new Intent(Intent.ACTION_SENDTO);
31 | email.setData(Uri.parse("mailto:"));
32 | //邮件主题
33 | if (!TextUtils.isEmpty(data.getTitle())) {
34 | email.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
35 | }
36 | //邮件内容
37 | String contentt = data.getContent() + data.getUrl();
38 | email.putExtra(Intent.EXTRA_TEXT, contentt);
39 | if (ShareUtil.startActivity(context, email)) {
40 | if (null != listener) {
41 | listener.onShare(ShareConstant.SHARE_CHANNEL_EMAIL, ShareConstant.SHARE_STATUS_COMPLETE);
42 | }
43 | } else {
44 | if (null != listener) {
45 | listener.onShare(ShareConstant.SHARE_CHANNEL_EMAIL, ShareConstant.SHARE_STATUS_FAILED);
46 | }
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/channel/ShareBySystem.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.channel;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.text.TextUtils;
6 |
7 | import com.xyzlf.share.library.R;
8 | import com.xyzlf.share.library.bean.ShareEntity;
9 | import com.xyzlf.share.library.interfaces.OnShareListener;
10 | import com.xyzlf.share.library.interfaces.ShareConstant;
11 | import com.xyzlf.share.library.util.ShareUtil;
12 | import com.xyzlf.share.library.util.ToastUtil;
13 |
14 | /**
15 | * Created by zhanglifeng
16 | */
17 | public class ShareBySystem extends ShareBase {
18 |
19 | public ShareBySystem(Context context) {
20 | super(context);
21 | }
22 |
23 | @Override
24 | public void share(ShareEntity data, OnShareListener listener) {
25 | if (data == null || TextUtils.isEmpty(data.getContent())) {
26 | ToastUtil.showToast(context, R.string.share_empty_tip, true);
27 | return;
28 | }
29 | String content;
30 | if (TextUtils.isEmpty(data.getContent())) {
31 | content = data.getTitle() + data.getUrl();
32 | } else {
33 | content = data.getContent() + data.getUrl();
34 | }
35 |
36 | Intent shareIntent = new Intent();
37 | shareIntent.setAction(Intent.ACTION_SEND);
38 | shareIntent.putExtra(Intent.EXTRA_TEXT, content);
39 | shareIntent.setType("text/plain");
40 | if(ShareUtil.startActivity(context, Intent.createChooser(
41 | shareIntent, context.getString(R.string.share_to)))) {
42 | if (null != listener) {
43 | listener.onShare(ShareConstant.SHARE_CHANNEL_SYSTEM, ShareConstant.SHARE_STATUS_COMPLETE);
44 | }
45 | } else {
46 | if (null != listener) {
47 | listener.onShare(ShareConstant.SHARE_CHANNEL_SYSTEM, ShareConstant.SHARE_STATUS_FAILED);
48 | }
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/channel/ShareBySms.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.channel;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.net.Uri;
6 | import android.text.TextUtils;
7 |
8 | import com.xyzlf.share.library.R;
9 | import com.xyzlf.share.library.bean.ShareEntity;
10 | import com.xyzlf.share.library.interfaces.OnShareListener;
11 | import com.xyzlf.share.library.interfaces.ShareConstant;
12 | import com.xyzlf.share.library.util.ShareUtil;
13 | import com.xyzlf.share.library.util.ToastUtil;
14 |
15 | /**
16 | * Created by zhanglifeng
17 | */
18 | public class ShareBySms extends ShareBase {
19 |
20 | public ShareBySms(Context context) {
21 | super(context);
22 | }
23 |
24 | @Override
25 | public void share(ShareEntity data, OnShareListener listener) {
26 | if (data == null || TextUtils.isEmpty(data.getContent())) {
27 | ToastUtil.showToast(context, R.string.share_empty_tip, true);
28 | return;
29 | }
30 | String content;
31 | if (TextUtils.isEmpty(data.getContent())) {
32 | content = data.getTitle() + data.getUrl();
33 | } else {
34 | content = data.getContent() + data.getUrl();
35 | }
36 |
37 | Uri smsToUri = Uri.parse("smsto:");
38 | Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri);
39 | //sendIntent.putExtra("address", "");
40 | //短信内容
41 | sendIntent.putExtra("sms_body", content);
42 | sendIntent.setType("vnd.android-dir/mms-sms");
43 | if (ShareUtil.startActivity(context, sendIntent)) {
44 | if (null != listener) {
45 | listener.onShare(ShareConstant.SHARE_CHANNEL_SMS, ShareConstant.SHARE_STATUS_COMPLETE);
46 | }
47 | } else {
48 | if (null != listener) {
49 | listener.onShare(ShareConstant.SHARE_CHANNEL_SMS, ShareConstant.SHARE_STATUS_FAILED);
50 | }
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/util/ChannelUtil.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.util;
2 |
3 | import android.content.Context;
4 | import android.content.pm.PackageManager;
5 | import android.text.TextUtils;
6 |
7 | import com.xyzlf.share.library.interfaces.ShareConstant;
8 |
9 | /**
10 | * Created by zhanglifeng on 2016/6/17
11 | */
12 | public class ChannelUtil {
13 | /**
14 | * 判断App是否安装
15 | *
16 | * @param context context
17 | * @param pkgName pkgname
18 | * @return boolean
19 | */
20 | public static boolean isAppInstall(Context context, String pkgName) {
21 | if (TextUtils.isEmpty(pkgName)) {
22 | return false;
23 | }
24 | try {
25 | PackageManager pm = context.getPackageManager();
26 | pm.getPackageInfo(pkgName, 0);
27 | return true;
28 | } catch (PackageManager.NameNotFoundException e) {
29 | e.printStackTrace();
30 | }
31 |
32 | return false;
33 | }
34 | /**
35 | * 微信是否安装
36 | * @param context context
37 | * @return boolean
38 | */
39 | public static boolean isWeixinInstall(Context context) {
40 | return isAppInstall(context, ShareConstant.WEIXIN_PACKAGE_NAME);
41 | }
42 | /**
43 | * QQ是否安装
44 | * @param context context
45 | * @return boolean
46 | */
47 | public static boolean isQQInstall(Context context) {
48 | return isAppInstall(context, ShareConstant.QQ_PACKAGE_NAME);
49 | }
50 |
51 | /**
52 | * 微博是否安装
53 | * @param context context
54 | * @return boolean
55 | */
56 | public static boolean isWeiboInstall(Context context) {
57 | return isAppInstall(context, ShareConstant.SINA_WEIBO_PACKAGE_NAME);
58 | }
59 | /**
60 | * @param context context
61 | * @return boolean
62 | */
63 | public static boolean isWeiboLiteInstall(Context context) {
64 | return isAppInstall(context, ShareConstant.SINA_WEIBO_LITE_PACKAGE_NAME);
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/shareLibrary/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.novoda.bintray-release'//??
3 |
4 |
5 | android {
6 | compileSdkVersion 28
7 | buildToolsVersion '28.0.3'
8 |
9 | defaultConfig {
10 | minSdkVersion 14
11 | targetSdkVersion 28
12 | versionCode 1
13 | versionName "0.0.1"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 |
22 | lintOptions {
23 | checkReleaseBuilds false
24 | // Or, if you prefer, you can continue to check for errors in release builds,
25 | // but continue the build even when errors are found:
26 | abortOnError false
27 | }
28 |
29 | packagingOptions {
30 | exclude 'META-INF/DEPENDENCIES.txt'
31 | exclude 'META-INF/LICENSE.txt'
32 | exclude 'META-INF/NOTICE.txt'
33 | exclude 'META-INF/NOTICE'
34 | exclude 'META-INF/LICENSE'
35 | exclude 'META-INF/DEPENDENCIES'
36 | exclude 'META-INF/notice.txt'
37 | exclude 'META-INF/license.txt'
38 | exclude 'META-INF/dependencies.txt'
39 | exclude 'META-INF/LGPL2.1'
40 | }
41 |
42 |
43 | }
44 |
45 |
46 | dependencies {
47 | implementation fileTree(dir: 'libs', include: ['*.jar'])
48 | implementation 'com.android.support:appcompat-v7:28.0.0'
49 | }
50 |
51 | Properties properties = new Properties()
52 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
53 |
54 | String localBintrayUser = properties.getProperty("bintray.user")
55 | String localBintrayApikey = properties.getProperty("bintray.apikey")
56 |
57 | //????
58 | //gradlew clean build bintrayUpload
59 | //??
60 | publish {
61 | bintrayUser = localBintrayUser //bintray.com???
62 | bintrayKey = localBintrayApikey //bintray.com apikey
63 | dryRun = false
64 | userOrg = localBintrayUser
65 | groupId = 'com.'+ localBintrayUser +'.share'//jcenter????
66 | artifactId = 'sharesdk'//????
67 | publishVersion = '0.0.11'//???
68 | desc = 'This is a share SDK. It include QQ, Weixin, Weibo, Sms, Email channel.'
69 | website = 'https://github.com/xyzlf/ShareSDK'
70 | }
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xyzlf/share/ShareCallBack.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share;
2 |
3 |
4 | import com.xyzlf.share.library.interfaces.ShareConstant;
5 |
6 | /**
7 | * Created by zhanglifeng on 2016/6/22
8 | */
9 | public class ShareCallBack {
10 |
11 | /**
12 | * @param channel
13 | * @param status
14 | */
15 | public void onShareCallback(int channel, int status) {
16 | switch (channel) {
17 | case ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND:
18 | case ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE:
19 | onWeixinCallBack(status);
20 | break;
21 |
22 | case ShareConstant.SHARE_CHANNEL_SINA_WEIBO:
23 | onWeiboCallBack(status);
24 | break;
25 |
26 | case ShareConstant.SHARE_CHANNEL_QQ:
27 | onQQCallBack(status);
28 | break;
29 |
30 | case ShareConstant.SHARE_CHANNEL_QZONE:
31 | onQZoneCallBack(status);
32 | break;
33 |
34 | case ShareConstant.SHARE_CHANNEL_SYSTEM:
35 | onSystemCallBack(status);
36 | break;
37 | }
38 | }
39 |
40 | /** 没有取消状态 **/
41 | private void onWeixinCallBack(int status) {
42 | switch (status) {
43 | /** 成功 **/
44 | case ShareConstant.SHARE_STATUS_COMPLETE:
45 |
46 | break;
47 | /** 失败 **/
48 | case ShareConstant.SHARE_STATUS_FAILED:
49 |
50 | break;
51 | }
52 | }
53 |
54 | /** 以下有成功,失败,取消三种回调结果 **/
55 | private void onWeiboCallBack(int status) {
56 | switch (status) {
57 | /** 成功 **/
58 | case ShareConstant.SHARE_STATUS_COMPLETE:
59 |
60 | break;
61 | /** 失败 **/
62 | case ShareConstant.SHARE_STATUS_FAILED:
63 |
64 | break;
65 | /** 取消 **/
66 | case ShareConstant.SHARE_STATUS_CANCEL:
67 |
68 | break;
69 | }
70 | }
71 |
72 | private void onQQCallBack(int status) {
73 |
74 | }
75 |
76 | private void onQZoneCallBack(int status) {
77 | }
78 | /** 以上有成功,失败,取消三种回调结果 **/
79 |
80 |
81 | /** 系统分享,回调结果没有实际意义,因为不能知道用户是否发送短信了,是否发送邮件了 **/
82 | private void onSystemCallBack(int status) {
83 | }
84 | /** 系统分享,回调结果没有实际意义,因为不能知道用户是否发送短信了,是否发送邮件了 **/
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/interfaces/ShareConstant.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.interfaces;
2 |
3 | /**
4 | * Created by zhanglifeng on 16/06/20
5 | */
6 | public class ShareConstant {
7 |
8 | /// -------------------------- 分享渠道 --------------------------
9 | /**
10 | * 微信好友分享渠道
11 | */
12 | public static final int SHARE_CHANNEL_WEIXIN_FRIEND = 1; //微信好友
13 | /**
14 | * weixin circle
15 | */
16 | public static final int SHARE_CHANNEL_WEIXIN_CIRCLE = 1 << 1;
17 | /**
18 | * 新浪微博渠道
19 | */
20 | public static final int SHARE_CHANNEL_SINA_WEIBO = 1 << 2; //新浪微博
21 | /**
22 | * QQ分享渠道
23 | */
24 | public static final int SHARE_CHANNEL_QQ = 1 << 3; //QQ
25 | /**
26 | * QQ空间渠道
27 | */
28 | public static final int SHARE_CHANNEL_QZONE = 1 << 4;//qzone分享
29 | /**
30 | * 短信分享渠道
31 | */
32 | public static final int SHARE_CHANNEL_SMS = 1 << 5; //短信
33 | /**
34 | * 邮箱分享渠道
35 | */
36 | public static final int SHARE_CHANNEL_EMAIL = 1 << 6; //邮箱
37 | /**
38 | * 更多渠道
39 | */
40 | public static final int SHARE_CHANNEL_SYSTEM = 1 << 10; //更多
41 | /**
42 | * All channel
43 | */
44 | public static final int SHARE_CHANNEL_ALL = ~0;
45 |
46 | /// -------------------------- 分享渠道 --------------------------
47 |
48 | /// <<<< share status
49 | public static final String EXTRA_SHARE_DATA = "extra_share_data";
50 | public static final String EXTRA_SHARE_CHANNEL = "extra_show_channel";
51 | public static final String EXTRA_SHARE_STATUS = "extra_share_status";
52 |
53 | public static final int SHARE_STATUS_COMPLETE = 1;
54 | public static final int SHARE_STATUS_FAILED = 2;
55 | public static final int SHARE_STATUS_CANCEL = 3;
56 | public static final int SHARE_STATUS_ERROR = 4;
57 |
58 | public static final int SHARE_WEIBO_AUTH_COMPLETE = 20;
59 | public static final int SHARE_WEIBO_AUTH_CANCEL = 21;
60 | public static final int SHARE_WEIBO_AUTH_ERROR = 22;
61 |
62 | /// <<<< share status
63 |
64 |
65 | /// <<< 微信回调
66 | public static final String ACTION_WEIXIN_CALLBACK = "com.financial360.nicaifu.share.action.WEIXIN_CALLBACK";
67 | public static final String EXTRA_WEIXIN_RESULT = "weixin_result";
68 | /// <<< 微信回调
69 |
70 | public static final String WEIXIN_PACKAGE_NAME = "com.tencent.mm"; // 微信包名
71 | public static final String QQ_PACKAGE_NAME = "com.tencent.mobileqq"; // QQ包名
72 | public static final String SINA_WEIBO_PACKAGE_NAME = "com.sina.weibo"; // 新浪微博
73 | public static final String SINA_WEIBO_LITE_PACKAGE_NAME = "com.sina.weibolite";
74 |
75 | /// <<<< request code
76 | public static final int REQUEST_CODE = 20112;
77 | /// <<<< request code
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/channel/ShareByQZone.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.channel;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.os.Bundle;
6 | import android.text.TextUtils;
7 |
8 | import com.tencent.connect.share.QzoneShare;
9 | import com.tencent.tauth.IUiListener;
10 | import com.tencent.tauth.UiError;
11 | import com.xyzlf.share.library.R;
12 | import com.xyzlf.share.library.bean.ShareEntity;
13 | import com.xyzlf.share.library.interfaces.OnShareListener;
14 | import com.xyzlf.share.library.interfaces.ShareConstant;
15 | import com.xyzlf.share.library.util.ToastUtil;
16 |
17 | import java.util.ArrayList;
18 |
19 | /**
20 | * Created by zhanglifeng
21 | */
22 | public class ShareByQZone extends ShareByQQ {
23 |
24 | public ShareByQZone(Context context) {
25 | super(context);
26 | }
27 |
28 | @Override
29 | public void share(ShareEntity data, final OnShareListener listener) {
30 | if (null == data) {
31 | return;
32 | }
33 | if (context == null || !(context instanceof Activity)) {
34 | return;
35 | }
36 | final Bundle params = new Bundle();
37 | params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
38 | params.putString(QzoneShare.SHARE_TO_QQ_TITLE, data.getTitle());//必填
39 | params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, data.getContent());//选填
40 | params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, data.getUrl());//必填
41 | ArrayList arrayList = new ArrayList<>();
42 | if (!TextUtils.isEmpty(data.getImgUrl())) {
43 | arrayList.add(data.getImgUrl());
44 | }
45 | params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, arrayList);
46 | mTencent.shareToQzone((Activity) context, params, new IUiListener() {
47 | @Override
48 | public void onComplete(Object o) {
49 | if (null != listener) {
50 | listener.onShare(ShareConstant.SHARE_CHANNEL_QZONE, ShareConstant.SHARE_STATUS_COMPLETE);
51 | }
52 | ToastUtil.showToast(context, R.string.share_success, true);
53 | }
54 | @Override
55 | public void onError(UiError uiError) {
56 | if (null != listener) {
57 | listener.onShare(ShareConstant.SHARE_CHANNEL_QZONE, ShareConstant.SHARE_STATUS_FAILED);
58 | }
59 | if (null != uiError) {
60 | ToastUtil.showToast(context, uiError.errorMessage, true);
61 | }
62 | }
63 | @Override
64 | public void onCancel() {
65 | if (null != listener) {
66 | listener.onShare(ShareConstant.SHARE_CHANNEL_QZONE, ShareConstant.SHARE_STATUS_CANCEL);
67 | }
68 | ToastUtil.showToast(context, R.string.share_cancel, true);
69 | }
70 | });
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
17 |
18 |
21 |
22 |
25 |
26 |
27 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
50 |
51 |
52 |
53 |
58 |
59 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/bean/ShareEntity.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.bean;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 | import android.support.annotation.DrawableRes;
6 |
7 | /**
8 | * Created by zhanglifeng
9 | */
10 | public class ShareEntity implements Parcelable {
11 |
12 | private String title;
13 | private String content;
14 | private String url;
15 |
16 | private String imgUrl;
17 | private int drawableId;
18 | private boolean isShareBigImg;
19 |
20 | public ShareEntity(String title, String content) {
21 | this(title, content, null);
22 | }
23 |
24 | public ShareEntity(String title, String content, String url) {
25 | this(title, content, url, null);
26 | }
27 |
28 | public ShareEntity(String title, String content, String url, String imgUrl) {
29 | this.title = title;
30 | this.content = content;
31 | this.url = url;
32 | this.imgUrl = imgUrl;
33 | }
34 |
35 | public String getTitle() {
36 | return title;
37 | }
38 |
39 | public void setTitle(String title) {
40 | this.title = title;
41 | }
42 |
43 | public String getContent() {
44 | return content;
45 | }
46 |
47 | public void setContent(String content) {
48 | this.content = content;
49 | }
50 |
51 | public String getUrl() {
52 | return url;
53 | }
54 |
55 | public void setUrl(String url) {
56 | this.url = url;
57 | }
58 |
59 | public String getImgUrl() {
60 | return imgUrl;
61 | }
62 |
63 | public void setImgUrl(String imgUrl) {
64 | this.imgUrl = imgUrl;
65 | }
66 |
67 | public void setDrawableId(@DrawableRes int drawableId) {
68 | this.drawableId = drawableId;
69 | }
70 |
71 | public int getDrawableId() {
72 | return drawableId;
73 | }
74 |
75 | public void setShareBigImg(boolean shareBigImg) {
76 | isShareBigImg = shareBigImg;
77 | }
78 |
79 | public boolean isShareBigImg() {
80 | return isShareBigImg;
81 | }
82 |
83 | @Override
84 | public int describeContents() {
85 | return 0;
86 | }
87 |
88 | public static final Creator CREATOR = new Creator() {
89 | @Override
90 | public ShareEntity createFromParcel(Parcel in) {
91 | return new ShareEntity(in);
92 | }
93 |
94 | @Override
95 | public ShareEntity[] newArray(int size) {
96 | return new ShareEntity[size];
97 | }
98 | };
99 |
100 | protected ShareEntity(Parcel in) {
101 | title = in.readString();
102 | content = in.readString();
103 | url = in.readString();
104 | imgUrl = in.readString();
105 | drawableId = in.readInt();
106 | isShareBigImg = in.readByte() != 0;
107 | }
108 |
109 | @Override
110 | public void writeToParcel(Parcel dest, int flags) {
111 | dest.writeString(title);
112 | dest.writeString(content);
113 | dest.writeString(url);
114 | dest.writeString(imgUrl);
115 | dest.writeInt(drawableId);
116 | dest.writeByte((byte) (isShareBigImg ? 1 : 0));
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/AccessTokenKeeper.java:
--------------------------------------------------------------------------------
1 | ///*
2 | // * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | // *
4 | // * Licensed under the Apache License, Version 2.0 (the "License");
5 | // * you may not use this file except in compliance with the License.
6 | // * You may obtain a copy of the License at
7 | // *
8 | // * http://www.apache.org/licenses/LICENSE-2.0
9 | // *
10 | // * Unless required by applicable law or agreed to in writing, software
11 | // * distributed under the License is distributed on an "AS IS" BASIS,
12 | // * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | // * See the License for the specific language governing permissions and
14 | // * limitations under the License.
15 | // */
16 | //
17 | //package com.xyzlf.share.library;
18 | //
19 | //import android.content.Context;
20 | //import android.content.SharedPreferences;
21 | //import android.content.SharedPreferences.Editor;
22 | //import android.os.Build;
23 | //
24 | //import com.sina.weibo.sdk.auth.Oauth2AccessToken;
25 | //
26 | //public class AccessTokenKeeper {
27 | // private static final String PREFERENCES_NAME = "com_weibo_sdk_android";
28 | //
29 | // private static final String KEY_UID = "uid";
30 | // private static final String KEY_ACCESS_TOKEN = "access_token";
31 | // private static final String KEY_EXPIRES_IN = "expires_in";
32 | // private static final String KEY_REFRESH_TOKEN = "refresh_token";
33 | //
34 | // public static void writeAccessToken(Context context, Oauth2AccessToken token) {
35 | // if (null == context || null == token) {
36 | // return;
37 | // }
38 | // SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
39 | // Editor editor = pref.edit();
40 | // editor.putString(KEY_UID, token.getUid());
41 | // editor.putString(KEY_ACCESS_TOKEN, token.getToken());
42 | // editor.putString(KEY_REFRESH_TOKEN, token.getRefreshToken());
43 | // editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime());
44 | // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
45 | // editor.apply();
46 | // } else {
47 | // editor.commit();
48 | // }
49 | // }
50 | //
51 | // public static Oauth2AccessToken readAccessToken(Context context) {
52 | // if (null == context) {
53 | // return null;
54 | // }
55 | // Oauth2AccessToken token = new Oauth2AccessToken();
56 | // SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
57 | // token.setUid(pref.getString(KEY_UID, ""));
58 | // token.setToken(pref.getString(KEY_ACCESS_TOKEN, ""));
59 | // token.setRefreshToken(pref.getString(KEY_REFRESH_TOKEN, ""));
60 | // token.setExpiresTime(pref.getLong(KEY_EXPIRES_IN, 0));
61 | //
62 | // return token;
63 | // }
64 | //
65 | // public static void clear(Context context) {
66 | // if (null == context) {
67 | // return;
68 | // }
69 | // SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
70 | // Editor editor = pref.edit();
71 | // editor.clear();
72 | // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
73 | // editor.apply();
74 | // } else {
75 | // editor.commit();
76 | // }
77 | // }
78 | //}
79 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/request/BitmapAsyncTask.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.request;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 |
6 | import java.io.BufferedInputStream;
7 | import java.io.IOException;
8 | import java.io.InputStream;
9 | import java.net.URL;
10 |
11 | /**
12 | * Created by zhanglifeng on 1/6/17
13 | */
14 | public class BitmapAsyncTask extends AbstractAsyncTask {
15 |
16 | private String urlStr;
17 | private OnBitmapListener listener;
18 |
19 | public BitmapAsyncTask(String urlStr, OnBitmapListener listener) {
20 | this.urlStr = urlStr;
21 | this.listener = listener;
22 | }
23 |
24 | @Override
25 | protected Bitmap doLoadData() throws Exception {
26 |
27 | URL url = new URL(urlStr);
28 | InputStream is = url.openStream();
29 | // 将InputStream变为Bitmap
30 | Bitmap bitmap = getSampleBitmap(is, 640, 640);
31 | is.close();
32 |
33 | return bitmap;
34 | }
35 |
36 | private Bitmap getSampleBitmap(InputStream is, int width, int height) {
37 |
38 | BufferedInputStream stream = new BufferedInputStream(is);
39 | stream.mark(4 * 1024);
40 | final BitmapFactory.Options options = new BitmapFactory.Options();
41 | options.inJustDecodeBounds = true;
42 | BitmapFactory.decodeStream(stream, null, options);
43 | calculateInSampleSize(width, height, options, true);
44 | try {
45 | stream.reset();
46 | } catch (IOException e) {
47 | }
48 | return BitmapFactory.decodeStream(stream, null, options);
49 | }
50 |
51 | static void calculateInSampleSize(int reqWidth, int reqHeight, BitmapFactory.Options options, boolean centerInside) {
52 | calculateInSampleSize(reqWidth, reqHeight, options.outWidth, options.outHeight, options,
53 | centerInside);
54 | }
55 |
56 | static void calculateInSampleSize(int reqWidth, int reqHeight, int width, int height,
57 | BitmapFactory.Options options, boolean centerInside) {
58 | int sampleSize = 1;
59 | if (height > reqHeight || width > reqWidth) {
60 | final int heightRatio;
61 | final int widthRatio;
62 | if (reqHeight == 0) {
63 | sampleSize = (int) Math.floor((float) width / (float) reqWidth);
64 | } else if (reqWidth == 0) {
65 | sampleSize = (int) Math.floor((float) height / (float) reqHeight);
66 | } else {
67 | heightRatio = (int) Math.floor((float) height / (float) reqHeight);
68 | widthRatio = (int) Math.floor((float) width / (float) reqWidth);
69 | sampleSize = centerInside
70 | ? Math.max(heightRatio, widthRatio)
71 | : Math.min(heightRatio, widthRatio);
72 | }
73 | }
74 | options.inSampleSize = sampleSize;
75 | options.inJustDecodeBounds = false;
76 | }
77 |
78 | @Override
79 | public void onSuccess(Bitmap bitmap) {
80 | super.onSuccess(bitmap);
81 | if (null != listener) {
82 | listener.onSuccess(bitmap);
83 | }
84 | }
85 |
86 | @Override
87 | public void onException(Exception exception) {
88 | super.onException(exception);
89 | if (null != listener) {
90 | listener.onException(exception);
91 | }
92 | }
93 |
94 | @Override
95 | public void onFinally() {
96 | super.onFinally();
97 | }
98 |
99 |
100 | public interface OnBitmapListener {
101 |
102 | void onSuccess(Bitmap bitmap);
103 |
104 | void onException(Exception exception);
105 | }
106 |
107 | }
108 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xyzlf/share/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.View;
7 |
8 | import com.xyzlf.share.library.bean.ShareEntity;
9 | import com.xyzlf.share.library.interfaces.ShareConstant;
10 | import com.xyzlf.share.library.util.ShareUtil;
11 |
12 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
13 |
14 | private ShareEntity testBean;
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_main);
20 |
21 | testBean = new ShareEntity("我是标题", "我是内容,描述内容。");
22 | testBean.setUrl("https://www.baidu.com"); //分享链接
23 | testBean.setImgUrl("https://www.baidu.com/img/bd_logo1.png");
24 |
25 | findViewById(R.id.share_qq).setOnClickListener(this);
26 | findViewById(R.id.share_qzone).setOnClickListener(this);
27 | findViewById(R.id.share_weibo).setOnClickListener(this);
28 | findViewById(R.id.share_weixin).setOnClickListener(this);
29 |
30 | findViewById(R.id.share_bigimage).setOnClickListener(this);
31 |
32 | }
33 |
34 | /**
35 | * 使用统一数据结构
36 | */
37 | public void showShareDialog(View view) {
38 | ShareEntity testBean = new ShareEntity("我是标题", "我是内容,描述内容。");
39 | testBean.setUrl("https://www.baidu.com"); //分享链接
40 | testBean.setImgUrl("https://www.baidu.com/img/bd_logo1.png");
41 | ShareUtil.showShareDialog(this, testBean, ShareConstant.REQUEST_CODE);
42 | }
43 |
44 | @Override
45 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
46 | super.onActivityResult(requestCode, resultCode, data);
47 | /**
48 | * 分享回调处理
49 | */
50 | if (requestCode == ShareConstant.REQUEST_CODE) {
51 | if (data != null) {
52 | int channel = data.getIntExtra(ShareConstant.EXTRA_SHARE_CHANNEL, -1);
53 | int status = data.getIntExtra(ShareConstant.EXTRA_SHARE_STATUS, -1);
54 | onShareCallback(channel, status);
55 | }
56 | }
57 | }
58 |
59 | /**
60 | * 分享回调处理
61 | * @param channel
62 | * @param status
63 | */
64 | private void onShareCallback(int channel, int status) {
65 | new ShareCallBack().onShareCallback(channel, status);
66 | }
67 |
68 | @Override
69 | public void onClick(View view) {
70 | int id = view.getId();
71 | switch (id) {
72 | case R.id.share_qq:
73 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_QQ, testBean, ShareConstant.REQUEST_CODE);
74 | break;
75 | case R.id.share_qzone:
76 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_QZONE, testBean, ShareConstant.REQUEST_CODE);
77 | break;
78 | case R.id.share_weibo:
79 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_SINA_WEIBO, testBean, ShareConstant.REQUEST_CODE);
80 | break;
81 | case R.id.share_weixin:
82 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND, testBean, ShareConstant.REQUEST_CODE);
83 | break;
84 | case R.id.share_wxcircle:
85 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE, testBean, ShareConstant.REQUEST_CODE);
86 | break;
87 |
88 | //分享大图
89 | case R.id.share_bigimage:
90 | shareBigImg();
91 | break;
92 | }
93 | }
94 |
95 | /**
96 | * 分享大图,大图分享支持,微信,微信朋友圈,微博,QQ,其他渠道不支持
97 | *
98 | * 分享大图注意点
99 | * 1、setShareBigImg为ture
100 | * 2、QQ分享大图,只能是本地图片
101 | */
102 | public void shareBigImg() {
103 | ShareEntity testBean = new ShareEntity("", "");
104 | testBean.setShareBigImg(true);
105 | // testBean.setImgUrl("https://www.baidu.com/img/bd_logo1.png"); // 网络地址
106 | testBean.setImgUrl("/storage/sdcard0/Android/data/com.xyzlf.share/files/com.xyzlf.share_share_pic.png"); //本地地址
107 |
108 | /** 如果你要分享的图片是Bitmap,你可以如下使用 **/
109 | // Bitmap bitmap = null;
110 | // String filePath = ShareUtil.saveBitmapToSDCard(this, bitmap);
111 | // testBean.setImgUrl(filePath);
112 |
113 | int channel = ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND | ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE | ShareConstant.SHARE_CHANNEL_SINA_WEIBO | ShareConstant.SHARE_CHANNEL_QQ;
114 | ShareUtil.showShareDialog(this, channel, testBean, ShareConstant.REQUEST_CODE);
115 | }
116 |
117 | }
118 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ShareSDK
2 | 分享SDK,包括微信、朋友圈,微博,QQ,QQ空间等渠道分享。
3 |
4 | # Gradle
5 |
6 | compile ('com.xyzlf.share:sharesdk:0.0.11') {
7 | exclude group: 'com.android.support', module: 'appcompat-v7'
8 | }
9 |
10 |
11 | # 效果图
12 |
13 |
14 | # 使用方式
15 |
16 | 1、调用分享界面:
17 |
18 | public void showShareDialog() {
19 | ShareEntity testBean = new ShareEntity("我是标题", "我是内容,描述内容。");
20 | testBean.setUrl("https://www.baidu.com"); //分享链接
21 | testBean.setImgUrl("https://www.baidu.com/img/bd_logo1.png");
22 | ShareUtil.showShareDialog(getActivity(), testBean, ShareConstant.REQUEST_CODE);
23 | }
24 |
25 | 2、使用分享功能,如下实例 使用QQ分享功能:
26 |
27 | public void startShare() {
28 | ShareEntity testBean = new ShareEntity("我是标题", "我是内容,描述内容。");
29 | testBean.setUrl("https://www.baidu.com"); //分享链接
30 | testBean.setImgUrl("https://www.baidu.com/img/bd_logo1.png");
31 | ShareUtil.startShare(getActivity(), ShareConstant.SHARE_CHANNEL_QQ, testBean, ShareConstant.REQUEST_CODE);
32 | }
33 |
34 | 3、处理回调:
35 |
36 | @Override
37 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
38 | super.onActivityResult(requestCode, resultCode, data);
39 | /**
40 | * 分享回调处理
41 | */
42 | if (requestCode == ShareConstant.REQUEST_CODE) {
43 | if (data != null) {
44 | int channel = data.getIntExtra(ShareConstant.EXTRA_SHARE_CHANNEL, -1);
45 | int status = data.getIntExtra(ShareConstant.EXTRA_SHARE_STATUS, -1);
46 | onShareCallback(channel, status);
47 | }
48 | }
49 | }
50 |
51 | 详细使用说明,可以参照上面的使用说明文档。
52 |
53 | # 遇到的坑
54 |
55 | 分享SDK踩过的坑,请参照:
56 |
57 | 配合使用说明文档,在配置调用方面应该不会有啥问题了。
58 |
59 | # 更新日志
60 |
61 | **0.0.11**
62 |
63 | 解决bug:Only fullscreen opaque activities can request orientation
64 |
65 | **0.0.9 Bugfixed**
66 |
67 | 解决qq未登录状态下,多次点击,多次弹出“分享取消”bug。
68 |
69 | **0.0.8 去除微博SDK**
70 |
71 | 使用0.0.8 aar,无需配置微博的key。 解决部分机型,微博service因隐士调用崩溃的问题。
72 |
73 | **0.0.7 支持分享大图,修复分享本地图片**
74 |
75 | ShareEntity testBean = new ShareEntity("我是标题", "我是内容,描述内容。");
76 |
77 | 1、去除了testBean.setBitmap(bitmap)方法,因为ShareEntity是通过Intent传递,如果bitmap稍大,就会出现异常。
78 |
79 | 2、增加了分享大图属性 testBean.setShareBigImg(true);
80 |
81 | 实例:
82 |
83 | /**
84 | * 分享大图,大图分享支持,微信,微信朋友圈,微博,QQ,其他渠道不支持
85 | *
86 | * 分享大图注意点
87 | * 1、setShareBigImg为ture
88 | * 2、QQ分享大图,只能是本地图片
89 | */
90 | public void shareBigImg() {
91 | ShareEntity testBean = new ShareEntity("", "");
92 | testBean.setShareBigImg(true);
93 | // testBean.setImgUrl("https://www.baidu.com/img/bd_logo1.png"); // 网络地址
94 | testBean.setImgUrl("/storage/sdcard0/Android/data/com.xyzlf.share/files/com.xyzlf.share_share_pic.png"); //本地地址
95 |
96 | /** 如果你要分享的图片是Bitmap,你可以如下使用 **/
97 | // Bitmap bitmap = null;
98 | // String filePath = ShareUtil.saveBitmapToSDCard(this, bitmap);
99 | // testBean.setImgUrl(filePath);
100 |
101 | int channel = ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND | ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE | ShareConstant.SHARE_CHANNEL_SINA_WEIBO | ShareConstant.SHARE_CHANNEL_QQ;
102 | ShareUtil.showShareDialog(this, channel, testBean, ShareConstant.REQUEST_CODE);
103 | }
104 |
105 |
106 | **0.0.6 支持本地图片分享**
107 |
108 | ShareEntity testBean = new ShareEntity("我是标题", "我是内容,描述内容。");
109 |
110 | 1、本地drawable图片,使用方式:testBean.setDrawableId(R.drawable.icon);
111 |
112 | 2、本地手机SD卡图片,使用方式:testBean.setImgUrl("/sdpath/filepath/icon.png");
113 |
114 | 3、本地代码截屏Bitmap,使用方式:testBean.setBitmap(bitmap);
115 |
116 | # 同类分享库收集
117 |
118 | 1、ThirdParty login and share lib:
119 |
120 | 2、社会化登录分享工具库:
121 |
122 | 3、Bilibili/BiliShare:
123 |
124 | 4、maoruibin/ChinaShare:
125 |
126 | 5、JeasonWong/FlipShare:
127 |
128 | 6、kayan1990/ShareButton:
129 |
130 | 7、arvinljw/SocialHelper:
131 |
132 | # 关于我
133 | 有任何使用问题,可以给我发邮件:
134 |
135 | Author:张利峰
136 |
137 | E-mail:519578280@qq.com
138 |
139 | # License
140 |
141 | Copyright(c)2016 xyzlf Open Source Project
142 |
143 | Licensed under the Apache License, Version 2.0 (the "License");
144 | you may not use this file except in compliance with the License.
145 | You may obtain a copy of the License at
146 |
147 | http://www.apache.org/licenses/LICENSE-2.0
148 |
149 | Unless required by applicable law or agreed to in writing, software
150 | distributed under the License is distributed on an "AS IS" BASIS,
151 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
152 | See the License for the specific language governing permissions and
153 | limitations under the License.
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/util/ManifestUtil.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.util;
2 |
3 | import android.content.Context;
4 | import android.content.pm.ApplicationInfo;
5 | import android.content.pm.PackageManager;
6 | import android.os.Bundle;
7 | import android.text.TextUtils;
8 |
9 | /**
10 | * Created by zhanglifeng.
11 | */
12 | public class ManifestUtil {
13 |
14 | public static final String SINA_WEIBO_KEY = "sina_weibo_key";
15 | public static final String SINA_WEIBO_REDIRECTURI = "sina_weibo_redirecturi";
16 | public static final String WEIXIN_KEY = "weixin_key";
17 | public static final String WEIXIN_REDIRECTURI_KEY = "weixin_redirecturi";
18 | public static final String TENCENT_QQ_APPID = "tencent_qq_appid";
19 |
20 | /**
21 | * 读取新浪微博的Appkey
22 | * @param context context
23 | * @return String
24 | */
25 | public static String getSinaWeiboKey(Context context) {
26 |
27 | try {
28 | ApplicationInfo appInfo = context.getPackageManager()
29 | .getApplicationInfo(context.getPackageName(),
30 | PackageManager.GET_META_DATA);
31 | if(null != appInfo) {
32 | Bundle bundle = appInfo.metaData;
33 | String key = null;
34 | if(null != bundle) {
35 | key = String.valueOf(bundle.get(SINA_WEIBO_KEY));
36 | }
37 | if(!TextUtils.isEmpty(key)) {
38 | return key;
39 | }
40 | }
41 | } catch (Exception e) {
42 | e.printStackTrace();
43 | }
44 | return "";
45 | }
46 |
47 | /**
48 | * 读取新浪微博的重定向URI
49 | * @param context context
50 | * @return String
51 | */
52 | public static String getSinaWeiboRedirecturi(Context context) {
53 | try {
54 | ApplicationInfo appInfo = context.getPackageManager()
55 | .getApplicationInfo(context.getPackageName(),
56 | PackageManager.GET_META_DATA);
57 |
58 | if(null != appInfo) {
59 | Bundle bundle = appInfo.metaData;
60 | String url = null;
61 | if(null != bundle) {
62 | url = bundle.getString(SINA_WEIBO_REDIRECTURI);
63 | }
64 | if(!TextUtils.isEmpty(url)) {
65 | return url;
66 | }
67 | }
68 | } catch (Exception e) {
69 | e.printStackTrace();
70 | }
71 | return "";
72 | }
73 |
74 | /**
75 | * 读取微信的AppKey
76 | * @param context context
77 | * @return String
78 | */
79 | public static String getWeixinKey(Context context) {
80 | try {
81 | ApplicationInfo appInfo = context.getPackageManager()
82 | .getApplicationInfo(context.getPackageName(),
83 | PackageManager.GET_META_DATA);
84 |
85 | if(null != appInfo) {
86 | Bundle bundle = appInfo.metaData;
87 | String key = null;
88 | if(null != bundle) {
89 | key = String.valueOf(bundle.get(WEIXIN_KEY));
90 | }
91 | if(!TextUtils.isEmpty(key)) {
92 | return key;
93 | }
94 | }
95 | } catch (Exception e) {
96 | e.printStackTrace();
97 | }
98 | return "";
99 | }
100 |
101 | /**
102 | * 读取微信的重定向URI
103 | * @param context context
104 | * @return String
105 | */
106 | public static String getWeixinRedirecturi(Context context) {
107 | try {
108 | ApplicationInfo appInfo = context.getPackageManager()
109 | .getApplicationInfo(context.getPackageName(),
110 | PackageManager.GET_META_DATA);
111 | if(null != appInfo) {
112 | Bundle bundle = appInfo.metaData;
113 | String url = null;
114 | if(null != bundle) {
115 | url = bundle.getString(WEIXIN_REDIRECTURI_KEY);
116 | }
117 |
118 | if(!TextUtils.isEmpty(url)) {
119 | return url;
120 | }
121 | }
122 | } catch (Exception e) {
123 | e.printStackTrace();
124 | }
125 | return "";
126 | }
127 |
128 | /**
129 | * tencent Appid
130 | * @param context context
131 | * @return String
132 | */
133 | public static String getTencentQQAppId(Context context) {
134 | try {
135 | ApplicationInfo appInfo = context.getPackageManager()
136 | .getApplicationInfo(context.getPackageName(),
137 | PackageManager.GET_META_DATA);
138 |
139 | if(null != appInfo) {
140 | Bundle bundle = appInfo.metaData;
141 | String key = null;
142 | if(null != bundle) {
143 | key = String.valueOf(bundle.get(TENCENT_QQ_APPID));
144 | }
145 |
146 | if(!TextUtils.isEmpty(key)) {
147 | return key;
148 | }
149 | }
150 | } catch (Exception e) {
151 | e.printStackTrace();
152 | }
153 | return "222222";
154 | }
155 |
156 | }
157 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/channel/ShareByQQ.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.channel;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.ResolveInfo;
7 | import android.os.Bundle;
8 | import android.text.TextUtils;
9 |
10 | import com.tencent.connect.share.QQShare;
11 | import com.tencent.tauth.IUiListener;
12 | import com.tencent.tauth.Tencent;
13 | import com.tencent.tauth.UiError;
14 | import com.xyzlf.share.library.R;
15 | import com.xyzlf.share.library.bean.ShareEntity;
16 | import com.xyzlf.share.library.interfaces.OnShareListener;
17 | import com.xyzlf.share.library.interfaces.ShareConstant;
18 | import com.xyzlf.share.library.util.ManifestUtil;
19 | import com.xyzlf.share.library.util.ShareUtil;
20 | import com.xyzlf.share.library.util.ToastUtil;
21 |
22 | import java.util.List;
23 |
24 | /**
25 | * Created by zhanglifeng
26 | */
27 | public class ShareByQQ extends ShareBase {
28 |
29 | protected Tencent mTencent;
30 |
31 | public ShareByQQ(Context context) {
32 | super(context);
33 | mTencent = Tencent.createInstance(ManifestUtil.getTencentQQAppId(context.getApplicationContext()), context.getApplicationContext());
34 | }
35 |
36 | @Override
37 | public void share(ShareEntity data, final OnShareListener listener) {
38 | if (null == data) {
39 | return;
40 | }
41 | if (context == null) {
42 | return;
43 | }
44 | IUiListener callBack = new IUiListener() {
45 | @Override
46 | public void onComplete(Object o) {
47 | if (null != listener) {
48 | listener.onShare(ShareConstant.SHARE_CHANNEL_QQ, ShareConstant.SHARE_STATUS_COMPLETE);
49 | }
50 | ToastUtil.showToast(context, R.string.share_success, true);
51 | }
52 |
53 | @Override
54 | public void onError(UiError uiError) {
55 | if (null != listener) {
56 | listener.onShare(ShareConstant.SHARE_CHANNEL_QQ, ShareConstant.SHARE_STATUS_FAILED);
57 | }
58 | if (null != uiError) {
59 | ToastUtil.showToast(context, uiError.errorMessage, true);
60 | }
61 | }
62 |
63 | @Override
64 | public void onCancel() {
65 | if (null != listener) {
66 | listener.onShare(ShareConstant.SHARE_CHANNEL_QQ, ShareConstant.SHARE_STATUS_CANCEL);
67 | }
68 | ToastUtil.showToast(context, R.string.share_cancel, true);
69 | }
70 | };
71 |
72 | if (data.isShareBigImg() && !TextUtils.isEmpty(data.getImgUrl()) && !data.getImgUrl().startsWith("http")) {
73 | Bundle params = new Bundle();
74 | params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, data.getImgUrl());
75 | params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
76 | params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE);
77 | mTencent.shareToQQ((Activity) context, params, callBack);
78 | } else {
79 | if (!TextUtils.isEmpty(data.getUrl()) && !TextUtils.isEmpty(data.getTitle())) {
80 | if (!(context instanceof Activity)) {
81 | return;
82 | }
83 | final Bundle params = new Bundle();
84 | params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
85 | params.putString(QQShare.SHARE_TO_QQ_TITLE, data.getTitle());//必填
86 | params.putString(QQShare.SHARE_TO_QQ_SUMMARY, data.getContent());//选填
87 | params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, data.getUrl());//必填
88 | if (!TextUtils.isEmpty(data.getImgUrl())) {
89 | params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, data.getImgUrl());
90 | }
91 | mTencent.shareToQQ((Activity) context, params, callBack);
92 | } else {
93 | String activityName = "";
94 | Intent intentSend = new Intent(Intent.ACTION_SEND);
95 | intentSend.setType("text/plain");
96 | List listActivity = context.getPackageManager().queryIntentActivities(intentSend, 0);
97 | for (ResolveInfo resolveInfo : listActivity) {
98 | if (TextUtils.equals(ShareConstant.QQ_PACKAGE_NAME, resolveInfo.activityInfo.packageName)) {
99 | activityName = resolveInfo.activityInfo.name;
100 | break;
101 | }
102 | }
103 | Intent qqIntent = new Intent(Intent.ACTION_SEND);
104 | qqIntent.setType("text/plain");
105 | qqIntent.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
106 | qqIntent.setClassName(ShareConstant.QQ_PACKAGE_NAME, activityName);
107 | qqIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
108 | qqIntent.putExtra(Intent.EXTRA_TEXT, data.getContent());
109 |
110 | if (ShareUtil.startActivity(context, qqIntent)) {
111 | if (null != listener) {
112 | listener.onShare(ShareConstant.SHARE_CHANNEL_QQ, ShareConstant.SHARE_STATUS_COMPLETE);
113 | }
114 | } else {
115 | if (null != listener) {
116 | listener.onShare(ShareConstant.SHARE_CHANNEL_QQ, ShareConstant.SHARE_STATUS_FAILED);
117 | }
118 | }
119 | }
120 | }
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/util/ShareUtil.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.util;
2 |
3 | import android.app.Activity;
4 | import android.content.ActivityNotFoundException;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.graphics.Bitmap;
8 | import android.os.Bundle;
9 | import android.os.Environment;
10 | import android.text.TextUtils;
11 | import android.util.SparseArray;
12 |
13 | import com.xyzlf.share.library.R;
14 | import com.xyzlf.share.library.ShareDialogActivity;
15 | import com.xyzlf.share.library.ShareHandlerActivity;
16 | import com.xyzlf.share.library.bean.ShareEntity;
17 | import com.xyzlf.share.library.interfaces.ShareConstant;
18 |
19 | import java.io.File;
20 | import java.io.FileOutputStream;
21 |
22 | /**
23 | * Created by zhanglifeng on 15/6/4.
24 | */
25 | public class ShareUtil {
26 |
27 | /**
28 | * 分享数据
29 | * @param activity Activity
30 | * @param channel {@link ShareConstant}
31 | * @param data {@link ShareEntity}
32 | * @param requestCode {@link android.app.Activity#onActivityResult(int, int, Intent)}
33 | */
34 | public static void startShare(Activity activity, int channel, ShareEntity data, int requestCode) {
35 | if (null == activity || activity.isFinishing()) {
36 | return;
37 | }
38 | Intent intent = new Intent(activity, ShareHandlerActivity.class);
39 | intent.putExtra(ShareConstant.EXTRA_SHARE_CHANNEL, channel);
40 | intent.putExtra(ShareConstant.EXTRA_SHARE_DATA, data);
41 | activity.startActivityForResult(intent, requestCode);
42 | }
43 |
44 | /**
45 | * 调起ShareDialogActivity
46 | * @param activity Activity
47 | * @param data {@link ShareEntity}
48 | * @param requestCode requestCode
49 | */
50 | public static void showShareDialog(Activity activity, ShareEntity data, int requestCode) {
51 | showShareDialog(activity, ShareConstant.SHARE_CHANNEL_ALL, data, requestCode);
52 | }
53 |
54 | /**
55 | * 调起ShareDialogActivity
56 | * @param activity Activity
57 | * @param data {@link ShareEntity}
58 | * @param channel {@link ShareConstant#SHARE_CHANNEL_ALL}
59 | * @param requestCode requestCode
60 | */
61 | public static void showShareDialog(Activity activity, int channel, ShareEntity data, int requestCode) {
62 | if (null == activity || activity.isFinishing()) {
63 | return;
64 | }
65 | Intent intent = new Intent(activity, ShareDialogActivity.class);
66 | intent.putExtra(ShareConstant.EXTRA_SHARE_DATA, data);
67 | intent.putExtra(ShareConstant.EXTRA_SHARE_CHANNEL, channel);
68 | activity.startActivityForResult(intent, requestCode);
69 | }
70 |
71 | /**
72 | * data is sparsearray
73 | * @param activity {@link Activity}
74 | * @param data {@link ShareEntity}
75 | * @param requestCode requestCode
76 | */
77 | public static void showShareDialog(Activity activity, SparseArray data, int requestCode) {
78 | showShareDialog(activity, ShareConstant.SHARE_CHANNEL_ALL, data, requestCode);
79 | }
80 |
81 | /**
82 | * data is sparsearray
83 | * @param activity Activity
84 | * @param channel 渠道
85 | * @param data data
86 | * @param requestCode requestCode
87 | */
88 | public static void showShareDialog(Activity activity, int channel, SparseArray data, int requestCode) {
89 | if (null == activity || activity.isFinishing()) {
90 | return;
91 | }
92 | Intent intent = new Intent(activity, ShareDialogActivity.class);
93 | Bundle bundle = new Bundle();
94 | bundle.putSparseParcelableArray(ShareConstant.EXTRA_SHARE_DATA, data);
95 | intent.putExtra(ShareConstant.EXTRA_SHARE_DATA, bundle);
96 | intent.putExtra(ShareConstant.EXTRA_SHARE_CHANNEL, channel);
97 | activity.startActivityForResult(intent, requestCode);
98 | }
99 |
100 |
101 | public static boolean startActivity(Context context, Class> c) {
102 | boolean result = true;
103 | try {
104 | Intent intent = new Intent(context, c);
105 | context.startActivity(intent);
106 | } catch (ActivityNotFoundException exception) {
107 | result = false;
108 | exception.printStackTrace();
109 | } catch (Exception e) {
110 | result = false;
111 | e.printStackTrace();
112 | }
113 | return result;
114 | }
115 |
116 | public static boolean startActivity(Context context, Intent intent) {
117 | boolean bResult = true;
118 | try {
119 | context.startActivity(intent);
120 | } catch (ActivityNotFoundException e) {
121 | bResult = false;
122 | e.printStackTrace();
123 | } catch (Exception e) {
124 | bResult = false;
125 | e.printStackTrace();
126 | }
127 | return bResult;
128 | }
129 |
130 | /**
131 | * save the Bitmap to SDCard
132 | * @param context context
133 | * @param bitmap bitmap
134 | * @return filePath
135 | */
136 | public static String saveBitmapToSDCard(Context context, Bitmap bitmap) {
137 | if (null == context) {
138 | return null;
139 | }
140 | if (null == bitmap) {
141 | ToastUtil.showToast(context, R.string.share_save_bitmap_failed, true);
142 | return null;
143 | }
144 | //SDCard is valid
145 | if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
146 | ToastUtil.showToast(context, R.string.share_save_bitmap_no_sdcard, true);
147 | return null;
148 | }
149 | String filePath = null;
150 | File externalFilesDir = context.getExternalFilesDir(null);
151 | String dir = null;
152 | if (null != externalFilesDir) {
153 | dir = externalFilesDir.getAbsolutePath();
154 | }
155 | String packageName = context.getPackageName();
156 | if (!TextUtils.isEmpty(dir)) {
157 | if (!dir.endsWith(File.separator)) {
158 | filePath = dir + File.separator + packageName + "_share_pic.png";
159 | } else {
160 | filePath = dir + packageName + "_share_pic.png";
161 | }
162 | try {
163 | File file = new File(filePath);
164 | if (file.exists()) {
165 | file.delete();
166 | }
167 | file.createNewFile();
168 |
169 | FileOutputStream outputStream = new FileOutputStream(file);
170 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
171 | outputStream.flush();
172 | outputStream.close();
173 | } catch (Exception e) {
174 | ToastUtil.showToast(context, e.getMessage(), true);
175 | }
176 | }
177 | return filePath;
178 | }
179 |
180 | }
181 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/ShareHandlerActivity.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.view.Window;
6 |
7 | import com.tencent.connect.common.Constants;
8 | import com.tencent.tauth.Tencent;
9 | import com.xyzlf.share.library.bean.ShareEntity;
10 | import com.xyzlf.share.library.channel.ShareByEmail;
11 | import com.xyzlf.share.library.channel.ShareByQQ;
12 | import com.xyzlf.share.library.channel.ShareByQZone;
13 | import com.xyzlf.share.library.channel.ShareBySms;
14 | import com.xyzlf.share.library.channel.ShareBySystem;
15 | import com.xyzlf.share.library.channel.ShareByWeibo2;
16 | import com.xyzlf.share.library.channel.ShareByWeixin;
17 | import com.xyzlf.share.library.interfaces.OnShareListener;
18 | import com.xyzlf.share.library.interfaces.ShareConstant;
19 |
20 | /**
21 | * Created by zhanglifeng on 16/06/20
22 | *
23 | * 分发Activity,主要对分享功能进行分发
24 | */
25 | public class ShareHandlerActivity extends ShareBaseActivity implements /*IWeiboHandler.Response, WeiboAuthListener,*/ OnShareListener {
26 |
27 | protected ShareEntity data;
28 |
29 | protected ShareByWeibo2 shareByWeibo;
30 | protected ShareByWeixin shareByWeixin;
31 | protected boolean isInit = true;
32 |
33 | // protected boolean isWeiboAuthComplete;
34 |
35 | @Override
36 | protected void onCreate(Bundle savedInstanceState) {
37 | requestWindowFeature(Window.FEATURE_NO_TITLE);
38 | super.onCreate(savedInstanceState);
39 | Object object = null;
40 | try {
41 | //Fuzz问题处理
42 | object = getIntent().getParcelableExtra(ShareConstant.EXTRA_SHARE_DATA);
43 | } catch (Exception e) {}
44 | if (null == object || !(object instanceof ShareEntity)) {
45 | finish();
46 | return;
47 | }
48 | data = (ShareEntity) object;
49 |
50 | if (savedInstanceState == null) {
51 |
52 | if (null != shareByWeixin) {
53 | shareByWeixin.unregisterWeixinReceiver();
54 | shareByWeixin = null;
55 | }
56 | switch (channel) {
57 | case ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE:
58 | shareByWeixin = new ShareByWeixin(this, ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE);
59 | shareByWeixin.registerWeixinReceiver();
60 | shareByWeixin.share(data, this);
61 | break;
62 |
63 | case ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND:
64 | shareByWeixin = new ShareByWeixin(this, ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND);
65 | shareByWeixin.registerWeixinReceiver();
66 | shareByWeixin.share(data, this);
67 | break;
68 |
69 | case ShareConstant.SHARE_CHANNEL_SINA_WEIBO:
70 | shareByWeibo = new ShareByWeibo2(this);
71 | shareByWeibo.share(data, this);
72 | break;
73 |
74 | case ShareConstant.SHARE_CHANNEL_QQ:
75 | new ShareByQQ(this).share(data, this);
76 | break;
77 |
78 | case ShareConstant.SHARE_CHANNEL_QZONE:
79 | new ShareByQZone(this).share(data, this);
80 | break;
81 |
82 | case ShareConstant.SHARE_CHANNEL_SMS:
83 | new ShareBySms(this).share(data, this);
84 | break;
85 |
86 | case ShareConstant.SHARE_CHANNEL_EMAIL:
87 | new ShareByEmail(this).share(data, this);
88 | break;
89 |
90 | case ShareConstant.SHARE_CHANNEL_SYSTEM:
91 | new ShareBySystem(this).share(data, this);
92 | break;
93 |
94 | default:
95 | finishWithResult(channel, ShareConstant.SHARE_STATUS_ERROR);
96 | break;
97 | }
98 | } else {
99 | // if (null != shareByWeibo) {
100 | // shareByWeibo.onNewIntent(getIntent(), this);
101 | // }
102 | }
103 | }
104 |
105 | @Override
106 | protected void onSaveInstanceState(Bundle outState) {
107 | super.onSaveInstanceState(outState);
108 | }
109 |
110 | @Override
111 | protected void onRestoreInstanceState(Bundle savedInstanceState) {
112 | super.onRestoreInstanceState(savedInstanceState);
113 | }
114 |
115 | @Override
116 | protected void onResume() {
117 | super.onResume();
118 | if (!isInit /*&& !isWeiboAuthComplete*/) {
119 | finishWithResult(channel, ShareConstant.SHARE_STATUS_ERROR);
120 | } else {
121 | isInit = false;
122 | }
123 | }
124 |
125 | // @Override
126 | // protected void onNewIntent(Intent intent) {
127 | // super.onNewIntent(intent);
128 | // if (null != shareByWeibo) {
129 | // shareByWeibo.onNewIntent(intent, this);
130 | // }
131 | // }
132 |
133 | @Override
134 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
135 | super.onActivityResult(requestCode, resultCode, data);
136 | if (requestCode == Constants.REQUEST_QQ_SHARE || requestCode == Constants.REQUEST_QZONE_SHARE) {
137 | Tencent.onActivityResultData(requestCode, resultCode, data, null);
138 | }
139 | }
140 |
141 | @Override
142 | public void onShare(int channel, int status) {
143 | finishWithResult(channel, status);
144 | }
145 |
146 | /**
147 | * weibo call back
148 | */
149 | // @Override
150 | // public void onResponse(BaseResponse baseResp) {
151 | // if (baseResp != null) {
152 | // switch (baseResp.errCode) {
153 | // case WBConstants.ErrorCode.ERR_OK:
154 | // onShare(ShareConstant.SHARE_CHANNEL_SINA_WEIBO, ShareConstant.SHARE_STATUS_COMPLETE);
155 | // ToastUtil.showToast(this, R.string.share_success, true);
156 | // break;
157 | // case WBConstants.ErrorCode.ERR_CANCEL:
158 | // onShare(ShareConstant.SHARE_CHANNEL_SINA_WEIBO, ShareConstant.SHARE_STATUS_CANCEL);
159 | // ToastUtil.showToast(this, R.string.share_cancel, true);
160 | // break;
161 | // case WBConstants.ErrorCode.ERR_FAIL:
162 | // onShare(ShareConstant.SHARE_CHANNEL_SINA_WEIBO, ShareConstant.SHARE_STATUS_FAILED);
163 | // ToastUtil.showToast(this, getString(R.string.share_failed) + "。Error Message: " + baseResp.errMsg, false);
164 | // break;
165 | // }
166 | // }
167 | // }
168 | //
169 | // @Override
170 | // public void onComplete(Bundle bundle) {
171 | // isWeiboAuthComplete = true;
172 | // shareByWeibo = new ShareByWeibo(this, this);
173 | // shareByWeibo.share(data, this);
174 | // }
175 | //
176 | // @Override
177 | // public void onWeiboException(WeiboException e) {
178 | // finishWithResult(channel, ShareConstant.SHARE_WEIBO_AUTH_CANCEL);
179 | // }
180 | //
181 | // @Override
182 | // public void onCancel() {
183 | // finishWithResult(channel, ShareConstant.SHARE_WEIBO_AUTH_ERROR);
184 | // }
185 |
186 | @Override
187 | protected void onDestroy() {
188 | super.onDestroy();
189 |
190 | if (null != shareByWeixin) {
191 | shareByWeixin.unregisterWeixinReceiver();
192 | }
193 | }
194 | }
195 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/channel/ShareByWeibo2.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.channel;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.content.pm.PackageManager;
6 | import android.content.pm.ResolveInfo;
7 | import android.graphics.Bitmap;
8 | import android.graphics.BitmapFactory;
9 | import android.graphics.drawable.BitmapDrawable;
10 | import android.graphics.drawable.Drawable;
11 | import android.net.Uri;
12 | import android.support.v4.content.ContextCompat;
13 | import android.text.TextUtils;
14 |
15 | import com.xyzlf.share.library.R;
16 | import com.xyzlf.share.library.bean.ShareEntity;
17 | import com.xyzlf.share.library.interfaces.OnShareListener;
18 | import com.xyzlf.share.library.interfaces.ShareConstant;
19 | import com.xyzlf.share.library.request.AbstractAsyncTask;
20 | import com.xyzlf.share.library.request.BitmapAsyncTask;
21 | import com.xyzlf.share.library.util.ChannelUtil;
22 | import com.xyzlf.share.library.util.ShareUtil;
23 | import com.xyzlf.share.library.util.ToastUtil;
24 |
25 | import java.util.List;
26 |
27 | /**
28 | * Created by zhanglifeng on 2016/6/18
29 | */
30 | public class ShareByWeibo2 extends ShareBase {
31 |
32 | private ShareEntity data;
33 | private OnShareListener listener;
34 |
35 | public ShareByWeibo2(Context context) {
36 | super(context);
37 | this.context = context.getApplicationContext();
38 | }
39 |
40 | @Override
41 | public void share(final ShareEntity data, OnShareListener listener) {
42 | boolean isWeiboInstalled = ChannelUtil.isWeiboInstall(context);
43 | boolean isWeiboLiteInstalled = false;
44 | if (!isWeiboInstalled) {
45 | isWeiboLiteInstalled = ChannelUtil.isWeiboLiteInstall(context);
46 | }
47 | if (!isWeiboInstalled && !isWeiboLiteInstalled) {
48 | ToastUtil.showToast(context, R.string.share_no_weibo_client, true);
49 | listener.onShare(ShareConstant.SHARE_CHANNEL_SINA_WEIBO, ShareConstant.SHARE_STATUS_FAILED);
50 | return;
51 | }
52 | if (null == data) {
53 | listener.onShare(ShareConstant.SHARE_CHANNEL_SINA_WEIBO, ShareConstant.SHARE_STATUS_FAILED);
54 | return;
55 | }
56 | this.listener = listener;
57 | this.data = data;
58 | if (!TextUtils.isEmpty(data.getImgUrl())) {
59 | if (data.getImgUrl().startsWith("http")) {
60 | new BitmapAsyncTask(data.getImgUrl(), new BitmapAsyncTask.OnBitmapListener() {
61 | @Override
62 | public void onSuccess(final Bitmap bitmap) {
63 | localSyncTask(bitmap);
64 | }
65 |
66 | @Override
67 | public void onException(Exception exception) {
68 | localSyncTask(null);
69 | }
70 | }).execute();
71 | } else {
72 | localSyncTask();
73 | }
74 | } else if (data.getDrawableId() != 0) {
75 | BitmapDrawable drawable = null;
76 | try {
77 | drawable = (BitmapDrawable) ContextCompat.getDrawable(context, data.getDrawableId());
78 | } catch (Exception ignored) {
79 | }
80 | if (null != drawable) {
81 | localSyncTask(drawable.getBitmap());
82 | } else {
83 | localSyncTask(null);
84 | }
85 | } else {
86 | localSyncTask(null);
87 | }
88 | }
89 |
90 | private void localSyncTask(final Bitmap bitmap) {
91 | new AbstractAsyncTask() {
92 | @Override
93 | protected String doLoadData() throws Exception {
94 | String imgPath;
95 | if (null != bitmap) {
96 | imgPath = ShareUtil.saveBitmapToSDCard(context, bitmap);
97 | } else {
98 | imgPath = ShareUtil.saveBitmapToSDCard(context, getDefaultBitmap(context));
99 | }
100 | weiboShare(imgPath);
101 | return null;
102 | }
103 | }.execute();
104 | }
105 |
106 | private void localSyncTask() {
107 | if (!TextUtils.isEmpty(data.getImgUrl()) && !data.getImgUrl().startsWith("http")) {
108 | new AbstractAsyncTask() {
109 | @Override
110 | protected String doLoadData() throws Exception {
111 | weiboShare(data.getImgUrl());
112 | return null;
113 | }
114 | }.execute();
115 | } else {
116 | localSyncTask(null);
117 | }
118 | }
119 |
120 | private void weiboShare(final String imgPath) {
121 | Intent weiboIntent = new Intent(Intent.ACTION_SEND);
122 | weiboIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
123 | if (!TextUtils.isEmpty(imgPath)) {
124 | weiboIntent.setType("image/*");
125 | } else {
126 | weiboIntent.setType("text/plain");
127 | }
128 | try {
129 | String pkgName = "";
130 | PackageManager pm = context.getPackageManager();
131 | List matches = pm.queryIntentActivities(weiboIntent, PackageManager.MATCH_DEFAULT_ONLY);
132 | ResolveInfo info = null;
133 | for (ResolveInfo each : matches) {
134 | pkgName = each.activityInfo.applicationInfo.packageName;
135 | if (ShareConstant.SINA_WEIBO_PACKAGE_NAME.equals(pkgName) || ShareConstant.SINA_WEIBO_LITE_PACKAGE_NAME.equals(pkgName)) {
136 | info = each;
137 | break;
138 | }
139 | }
140 |
141 | if (null != info) {
142 | weiboIntent.setClassName(pkgName, info.activityInfo.name);
143 |
144 | StringBuilder builder = new StringBuilder();
145 | if (!TextUtils.isEmpty(data.getContent())) {
146 | builder.append(data.getContent());
147 | }
148 | if (!TextUtils.isEmpty(data.getUrl())) {
149 | builder.append(" ").append(data.getUrl());
150 | }
151 | if (!TextUtils.isEmpty(builder.toString())) {
152 | weiboIntent.putExtra(Intent.EXTRA_TEXT, builder.toString());
153 | }
154 |
155 | if (!TextUtils.isEmpty(imgPath)) {
156 | weiboIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imgPath));
157 | }
158 | context.startActivity(weiboIntent);
159 | listener.onShare(ShareConstant.SHARE_CHANNEL_SINA_WEIBO, ShareConstant.SHARE_STATUS_COMPLETE);
160 | } else {
161 | listener.onShare(ShareConstant.SHARE_CHANNEL_SINA_WEIBO, ShareConstant.SHARE_STATUS_FAILED);
162 | }
163 | } catch (Exception ignore) {
164 | listener.onShare(ShareConstant.SHARE_CHANNEL_SINA_WEIBO, ShareConstant.SHARE_STATUS_FAILED);
165 | }
166 | }
167 |
168 | private Bitmap getDefaultBitmap(Context context) {
169 | Drawable icon = null;
170 | try {
171 | icon = context.getPackageManager().getApplicationIcon(context.getPackageName());
172 | } catch (Exception e) {
173 | }
174 | if (null != icon && icon instanceof BitmapDrawable) {
175 | return ((BitmapDrawable) icon).getBitmap();
176 | }
177 |
178 | return BitmapFactory.decodeResource(context.getResources(), R.drawable.share_default);
179 | }
180 |
181 |
182 | }
183 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/channel/ShareByWeixin.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library.channel;
2 |
3 | import android.app.Activity;
4 | import android.content.BroadcastReceiver;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.content.IntentFilter;
8 | import android.graphics.Bitmap;
9 | import android.graphics.BitmapFactory;
10 | import android.graphics.drawable.BitmapDrawable;
11 | import android.support.v4.content.ContextCompat;
12 | import android.text.TextUtils;
13 |
14 | import com.tencent.mm.sdk.modelbase.BaseResp;
15 | import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
16 | import com.tencent.mm.sdk.modelmsg.WXImageObject;
17 | import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
18 | import com.tencent.mm.sdk.modelmsg.WXTextObject;
19 | import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
20 | import com.tencent.mm.sdk.openapi.IWXAPI;
21 | import com.tencent.mm.sdk.openapi.WXAPIFactory;
22 | import com.xyzlf.share.library.R;
23 | import com.xyzlf.share.library.bean.ShareEntity;
24 | import com.xyzlf.share.library.interfaces.OnShareListener;
25 | import com.xyzlf.share.library.interfaces.ShareConstant;
26 | import com.xyzlf.share.library.request.BitmapAsyncTask;
27 | import com.xyzlf.share.library.util.ManifestUtil;
28 | import com.xyzlf.share.library.util.ToastUtil;
29 |
30 | import java.io.ByteArrayOutputStream;
31 | import java.io.File;
32 |
33 | /**
34 | * Created by zhanglifeng
35 | */
36 | public class ShareByWeixin extends ShareBase {
37 |
38 | private int channel;
39 | private ShareEntity data;
40 | private OnShareListener listener;
41 | private IWXAPI api;
42 |
43 | private WeixinShareReceiver receiver;
44 |
45 | public ShareByWeixin(Context context, int channel) {
46 | super(context);
47 | this.context = context.getApplicationContext();
48 | this.channel = channel;
49 | api = WXAPIFactory.createWXAPI(context.getApplicationContext(), ManifestUtil.getWeixinKey(this.context));
50 | }
51 |
52 | /**
53 | * 注册微信回调广播
54 | */
55 | public void registerWeixinReceiver() {
56 | receiver = new WeixinShareReceiver();
57 | IntentFilter intentFilter = new IntentFilter();
58 | intentFilter.addAction(ShareConstant.ACTION_WEIXIN_CALLBACK);
59 | context.registerReceiver(receiver, intentFilter);
60 | }
61 |
62 | /**
63 | * unregister
64 | */
65 | public void unregisterWeixinReceiver() {
66 | if (null != context && null != receiver) {
67 | context.unregisterReceiver(receiver);
68 | }
69 | }
70 |
71 | @Override
72 | public void share(ShareEntity data, OnShareListener listener) {
73 | this.data = data;
74 | this.listener = listener;
75 | if (null == data) {
76 | return;
77 | }
78 | start();
79 | }
80 |
81 | /**
82 | * start share
83 | */
84 | private void start() {
85 | if (api.isWXAppInstalled()) {
86 | String imgUrl = data.getImgUrl();
87 | if (!TextUtils.isEmpty(imgUrl)) {
88 | // 网络图片
89 | if (imgUrl.startsWith("http")) {
90 | new BitmapAsyncTask(imgUrl, new BitmapAsyncTask.OnBitmapListener() {
91 | @Override
92 | public void onSuccess(Bitmap bitmap) {
93 | if (data.isShareBigImg()) {
94 | shareImg(bitmap, listener);
95 | } else {
96 | send(bitmap);
97 | }
98 | }
99 |
100 | @Override
101 | public void onException(Exception exception) {
102 | send();
103 | }
104 | }).execute();
105 | } else {
106 | //本地图片
107 | if (data.isShareBigImg()) {
108 | shareImg(getLoacalBitmap(imgUrl), listener);
109 | } else {
110 | send(getLoacalBitmap(imgUrl));
111 | }
112 | }
113 | } else if (data.getDrawableId() != 0) {
114 | BitmapDrawable drawable = null;
115 | try {
116 | drawable = (BitmapDrawable) ContextCompat.getDrawable(context, data.getDrawableId());
117 | } catch (Exception ignored) {
118 | }
119 | if (null != drawable) {
120 | send(drawable.getBitmap());
121 | } else {
122 | send();
123 | }
124 | }/* else if (data.getBitmap() != null) {
125 | send(data.getBitmap());
126 | } */ else {
127 | send();
128 | }
129 | } else {
130 | if (null != listener) {
131 | listener.onShare(channel, ShareConstant.SHARE_STATUS_FAILED);
132 | }
133 | ToastUtil.showToast(context, R.string.share_no_weixin_client, true);
134 | }
135 | }
136 |
137 | public Bitmap getLoacalBitmap(String localPath) {
138 | File file = new File(localPath);
139 | if (file.exists()) {
140 | try {
141 | return BitmapFactory.decodeFile(localPath);
142 | } catch (OutOfMemoryError error) {
143 | error.printStackTrace();
144 | }
145 | }
146 | return BitmapFactory.decodeResource(context.getResources(), R.drawable.share_default);
147 | }
148 |
149 | private void send() {
150 | this.send(null);
151 | }
152 |
153 | private void send(Bitmap bitmap) {
154 | SendMessageToWX.Req req = new SendMessageToWX.Req();
155 | req.transaction = String.valueOf(System.currentTimeMillis());
156 | req.message = buildWXMediaMessage(bitmap);
157 | if (channel == ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND) {
158 | req.scene = SendMessageToWX.Req.WXSceneSession;
159 | } else if (channel == ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE) {
160 | req.scene = SendMessageToWX.Req.WXSceneTimeline;
161 | }
162 | api.sendReq(req);
163 | }
164 |
165 | /**
166 | * 构建WXMediaMessage
167 | *
168 | * @param bitmap
169 | */
170 | private WXMediaMessage buildWXMediaMessage(Bitmap bitmap) {
171 | WXMediaMessage wxMediaMessage = new WXMediaMessage();
172 | wxMediaMessage.title = data.getTitle();
173 | wxMediaMessage.description = data.getContent();
174 | if (TextUtils.isEmpty(data.getUrl())) {
175 | wxMediaMessage.mediaObject = new WXTextObject(data.getContent());
176 | } else {
177 | if (null != bitmap) {
178 | wxMediaMessage.setThumbImage(getWxShareBitmap(bitmap));
179 | } else {
180 | Bitmap defautlBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.share_default);
181 | if (null != defautlBitmap) {
182 | wxMediaMessage.setThumbImage(getWxShareBitmap(defautlBitmap));
183 | }
184 | }
185 | wxMediaMessage.mediaObject = new WXWebpageObject(data.getUrl());
186 | }
187 | return wxMediaMessage;
188 | }
189 |
190 | /**
191 | * 微信分享图片不能超过64kb,特别坑...
192 | * @param targetBitmap bitmap
193 | * @return Bitmap
194 | */
195 | protected Bitmap getWxShareBitmap(Bitmap targetBitmap) {
196 | float scale = Math.min((float) 150 / targetBitmap.getWidth(), (float) 150 / targetBitmap.getHeight());
197 | Bitmap fixedBmp = Bitmap.createScaledBitmap(targetBitmap, (int) (scale * targetBitmap.getWidth()), (int) (scale * targetBitmap.getHeight()), false);
198 | return fixedBmp;
199 | }
200 |
201 | /**
202 | * 微信分享回调广播
203 | */
204 | private class WeixinShareReceiver extends BroadcastReceiver {
205 | @Override
206 | public void onReceive(Context context, Intent intent) {
207 | if (intent.hasExtra(ShareConstant.EXTRA_WEIXIN_RESULT)) {
208 | int errCode = intent.getIntExtra(ShareConstant.EXTRA_WEIXIN_RESULT, BaseResp.ErrCode.ERR_USER_CANCEL);
209 | int resultCode = (errCode == BaseResp.ErrCode.ERR_OK) ? Activity.RESULT_OK : Activity.RESULT_CANCELED;
210 | if (resultCode == Activity.RESULT_OK) {
211 | //分享成功
212 | if (null != listener) {
213 | listener.onShare(channel, ShareConstant.SHARE_STATUS_COMPLETE);
214 | }
215 | ToastUtil.showToast(context, R.string.share_success, true);
216 | } else {
217 | //分享失败
218 | if (null != listener) {
219 | listener.onShare(channel, ShareConstant.SHARE_STATUS_FAILED);
220 | }
221 | // ToastUtil.showToast(context, R.string.share_failed, true);
222 | }
223 | }
224 | }
225 | }
226 |
227 | private static final int THUMB_SIZE = 250;
228 |
229 | public void shareImg(Bitmap bitmap, OnShareListener listener) {
230 | if (null == bitmap) {
231 | if (null != listener) {
232 | listener.onShare(channel, ShareConstant.SHARE_STATUS_FAILED);
233 | }
234 | return;
235 | }
236 | if (api.isWXAppInstalled()) {
237 | if (api.isWXAppSupportAPI()) {
238 | WXMediaMessage wxMediaMessage = new WXMediaMessage();
239 | wxMediaMessage.mediaObject = new WXImageObject(bitmap);
240 |
241 | int width = bitmap.getWidth();
242 | int height = bitmap.getHeight();
243 | while (width * height > THUMB_SIZE * THUMB_SIZE) {
244 | width /= 2;
245 | height /= 2;
246 | }
247 | Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, width, height, true);
248 | try {
249 | ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
250 | thumbBmp.compress(Bitmap.CompressFormat.JPEG, 85, localByteArrayOutputStream);
251 | wxMediaMessage.thumbData = localByteArrayOutputStream.toByteArray();
252 | localByteArrayOutputStream.close();
253 | } catch (Exception e) {
254 |
255 | }
256 |
257 | SendMessageToWX.Req req = new SendMessageToWX.Req();
258 | req.transaction = String.valueOf(System.currentTimeMillis());
259 | req.message = wxMediaMessage;
260 | if (ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND == channel) {
261 | req.scene = SendMessageToWX.Req.WXSceneSession;
262 | } else if (ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE == channel) {
263 | req.scene = SendMessageToWX.Req.WXSceneTimeline;
264 | }
265 | api.sendReq(req);
266 | }
267 | } else {
268 | if (null != listener) {
269 | listener.onShare(channel, ShareConstant.SHARE_STATUS_FAILED);
270 | }
271 | ToastUtil.showToast(context, R.string.share_no_weixin_client, true);
272 | }
273 | }
274 |
275 | }
276 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/channel/ShareByWeibo.java:
--------------------------------------------------------------------------------
1 | //package com.xyzlf.share.library.channel;
2 | //
3 | //import android.app.Activity;
4 | //import android.content.Context;
5 | //import android.content.Intent;
6 | //import android.graphics.Bitmap;
7 | //import android.graphics.BitmapFactory;
8 | //import android.graphics.drawable.BitmapDrawable;
9 | //import android.os.Bundle;
10 | //import android.support.v4.content.ContextCompat;
11 | //import android.text.TextUtils;
12 | //
13 | //import com.sina.weibo.sdk.api.ImageObject;
14 | //import com.sina.weibo.sdk.api.TextObject;
15 | //import com.sina.weibo.sdk.api.WeiboMultiMessage;
16 | //import com.sina.weibo.sdk.api.share.IWeiboHandler;
17 | //import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
18 | //import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
19 | //import com.sina.weibo.sdk.api.share.WeiboShareSDK;
20 | //import com.sina.weibo.sdk.auth.AuthInfo;
21 | //import com.sina.weibo.sdk.auth.Oauth2AccessToken;
22 | //import com.sina.weibo.sdk.auth.WeiboAuthListener;
23 | //import com.sina.weibo.sdk.auth.sso.SsoHandler;
24 | //import com.sina.weibo.sdk.exception.WeiboException;
25 | //import com.xyzlf.com.share.library.R;
26 | //import com.xyzlf.share.library.AccessTokenKeeper;
27 | //import com.xyzlf.share.library.bean.ShareEntity;
28 | //import com.xyzlf.share.library.interfaces.OnShareListener;
29 | //import com.xyzlf.share.library.request.BitmapAsyncTask;
30 | //import com.xyzlf.share.library.util.ManifestUtil;
31 | //import com.xyzlf.share.library.util.ToastUtil;
32 | //
33 | //import java.io.File;
34 | //
35 | //
36 | ///**
37 | // * Created by zhanglifeng on 2016/6/18
38 | // */
39 | //public class ShareByWeibo extends ShareBase {
40 | //
41 | // public static final long HALF_HOUR = 30 * 60 * 1000L;
42 | // /**
43 | // * 微博微博分享接口实例
44 | // */
45 | // private IWeiboShareAPI mWeiboShareAPI = null;
46 | // private AuthInfo mAuthInfo;
47 | // /**
48 | // * if SsoHandler SDK support SSO
49 | // */
50 | // private SsoHandler mSsoHandler;
51 | // private WeiboAuthListener authListener;
52 | //
53 | // private ShareEntity data;
54 | // private String weiboToken;
55 | //
56 | // public ShareByWeibo(Context context, WeiboAuthListener authListener) {
57 | // super(context);
58 | // this.authListener = authListener;
59 | // final String weiboKey = ManifestUtil.getSinaWeiboKey(context);
60 | // final String redirect = ManifestUtil.getSinaWeiboRedirecturi(context);
61 | // // 创建微博分享接口实例
62 | // mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(context.getApplicationContext(), weiboKey);
63 | // mWeiboShareAPI.registerApp();
64 | //
65 | // mAuthInfo = new AuthInfo(context, weiboKey, redirect, "");
66 | // mSsoHandler = new SsoHandler((Activity) context, mAuthInfo);
67 | // }
68 | //
69 | // public void onNewIntent(Intent intent, IWeiboHandler.Response response) {
70 | // if (null != mWeiboShareAPI) {
71 | // mWeiboShareAPI.handleWeiboResponse(intent, response);
72 | // }
73 | // }
74 | //
75 | // public void onActivityResult(int requestCode, int resultCode, Intent data) {
76 | // if (requestCode == 32973) {
77 | // // private static final int REQUEST_CODE_SSO_AUTH = 32973;
78 | // if (null != mSsoHandler) {
79 | // // SSO 授权回调
80 | // mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
81 | // }
82 | // }
83 | // }
84 | //
85 | // @Override
86 | // public void share(ShareEntity data, OnShareListener listener) {
87 | // if (null == data) {
88 | // return;
89 | // }
90 | // this.data = data;
91 | // if (isSessionValid()) {
92 | // weiboShare();
93 | // } else {
94 | // mSsoHandler.authorize(new AuthListener());
95 | // }
96 | // }
97 | //
98 | // private void weiboShare() {
99 | // String imgUrl = data.getImgUrl();
100 | // if (!TextUtils.isEmpty(imgUrl)) {
101 | // // 网络图片
102 | // if (imgUrl.startsWith("http")) {
103 | // new BitmapAsyncTask(imgUrl, new BitmapAsyncTask.OnBitmapListener() {
104 | // @Override
105 | // public void onSuccess(Bitmap bitmap) {
106 | // sendMultiMessage(bitmap);
107 | // }
108 | //
109 | // @Override
110 | // public void onException(Exception exception) {
111 | // sendMultiMessage();
112 | // }
113 | // }).execute();
114 | // } else {
115 | // //本地图片
116 | // sendMultiMessage(getLoacalBitmap(imgUrl));
117 | // }
118 | // } else if (data.getDrawableId() != 0) {
119 | // BitmapDrawable drawable = null;
120 | // try {
121 | // drawable = (BitmapDrawable) ContextCompat.getDrawable(context, data.getDrawableId());
122 | // } catch (Exception ignored) {
123 | // }
124 | // if (null != drawable) {
125 | // sendMultiMessage(drawable.getBitmap());
126 | // } else {
127 | // sendMultiMessage();
128 | // }
129 | // } /*else if (data.getBitmap() != null) {
130 | // sendMultiMessage(data.getBitmap());
131 | // }*/ else {
132 | // sendMultiMessage();
133 | // }
134 | // }
135 | //
136 | // private boolean isSessionValid() {
137 | // Oauth2AccessToken mAccessToken = AccessTokenKeeper.readAccessToken(context);
138 | // if (null != mAccessToken && mAccessToken.isSessionValid() && !isExpired(mAccessToken.getExpiresTime())) {
139 | // return true;
140 | // }
141 | // return false;
142 | // }
143 | //
144 | // public Bitmap getLoacalBitmap(String localPath) {
145 | // File file = new File(localPath);
146 | // if (file.exists()) {
147 | // try {
148 | // return BitmapFactory.decodeFile(localPath);
149 | // } catch (OutOfMemoryError error) {
150 | // error.printStackTrace();
151 | // }
152 | // }
153 | // return BitmapFactory.decodeResource(context.getResources(), R.drawable.share_default);
154 | // }
155 | //
156 | // /**
157 | // * 是否过期;剩余时间不足半小时视为过期
158 | // * @param expiredAt date
159 | // * @return boolean
160 | // */
161 | // public boolean isExpired(long expiredAt) {
162 | // return System.currentTimeMillis() > (expiredAt - HALF_HOUR);
163 | // }
164 | //
165 | // private void sendMultiMessage() {
166 | // this.sendMultiMessage(null);
167 | // }
168 | //
169 | // private void sendMultiMessage(Bitmap bitmap) {
170 | // // 初始化微博的分享消息
171 | // WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
172 | // weiboMessage.textObject = getTextObj(data);
173 | //
174 | // if (null != bitmap) {
175 | // weiboMessage.imageObject = getImageObj(bitmap);
176 | // }
177 | //
178 | // SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
179 | // request.transaction = String.valueOf(System.currentTimeMillis());
180 | // request.multiMessage = weiboMessage;
181 | //
182 | // String token = "";
183 | // if (!TextUtils.isEmpty(weiboToken)) {
184 | // token = weiboToken;
185 | // } else {
186 | // Oauth2AccessToken mAccessToken = AccessTokenKeeper.readAccessToken(context);
187 | // if (mAccessToken != null) {
188 | // token = mAccessToken.getToken();
189 | // }
190 | // }
191 | // mWeiboShareAPI.sendRequest((Activity) context, request, mAuthInfo, token, new WeiboAuthListener() {
192 | //
193 | // @Override
194 | // public void onWeiboException(WeiboException arg0) {
195 | // ToastUtil.showToast(context, R.string.share_failed, true);
196 | // }
197 | //
198 | // @Override
199 | // public void onComplete(Bundle bundle) {
200 | // Oauth2AccessToken newToken = Oauth2AccessToken.parseAccessToken(bundle);
201 | // AccessTokenKeeper.writeAccessToken(context, newToken);
202 | // ToastUtil.showToast(context, R.string.share_success, true);
203 | // }
204 | //
205 | // @Override
206 | // public void onCancel() {
207 | // ToastUtil.showToast(context, R.string.share_cancel, true);
208 | // }
209 | // });
210 | // }
211 | //
212 | // private TextObject getTextObj(ShareEntity data) {
213 | // TextObject textObject = new TextObject();
214 | // if (null != data) {
215 | // StringBuilder builder = new StringBuilder();
216 | // if (!TextUtils.isEmpty(data.getContent())) {
217 | // builder.append(data.getContent());
218 | // }
219 | // if (!TextUtils.isEmpty(data.getUrl())) {
220 | // builder.append(" ").append(data.getUrl());
221 | // }
222 | // textObject.text = builder.toString();
223 | // }
224 | // return textObject;
225 | // }
226 | //
227 | // private ImageObject getImageObj(Bitmap bitmap) {
228 | // ImageObject imageObject = new ImageObject();
229 | // if (null != bitmap) {
230 | // imageObject.setImageObject(/*getShareBitmap(bitmap)*/bitmap);
231 | // } else {
232 | //// Bitmap localBitmap = getShareBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.share_default));
233 | // Bitmap localBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.share_default);
234 | // imageObject.setImageObject(localBitmap);
235 | // }
236 | // return imageObject;
237 | // }
238 | //
239 | //// /**
240 | //// * 微信分享图片不能超过32kb
241 | //// * @param targetBitmap bitmap
242 | //// * @return Bitmap
243 | //// */
244 | //// protected Bitmap getShareBitmap(Bitmap targetBitmap) {
245 | //// float scale = Math.min((float) 150 / targetBitmap.getWidth(), (float) 150 / targetBitmap.getHeight());
246 | //// Bitmap fixedBmp = Bitmap.createScaledBitmap(targetBitmap, (int) (scale * targetBitmap.getWidth()), (int) (scale * targetBitmap.getHeight()), false);
247 | //// return fixedBmp;
248 | //// }
249 | //
250 | // class AuthListener implements WeiboAuthListener {
251 | //
252 | // @Override
253 | // public void onComplete(Bundle values) {
254 | // Oauth2AccessToken accessToken = Oauth2AccessToken.parseAccessToken(values);
255 | // if (accessToken.isSessionValid()) {
256 | // weiboToken = accessToken.getToken();
257 | // AccessTokenKeeper.writeAccessToken(context, accessToken);
258 | // ToastUtil.showToast(context, R.string.share_weibosdk_auth_success, true);
259 | //
260 | // if (null != authListener) {
261 | // authListener.onComplete(values);
262 | // } else {
263 | // onCancel();
264 | // }
265 | // } else {
266 | // String code = values.getString("code");
267 | // String message = context.getString(R.string.share_weibosdk_auth_failed);
268 | // if (!TextUtils.isEmpty(code)) {
269 | // message = message + "\nObtained the code: " + code;
270 | // }
271 | // ToastUtil.showToast(context, message, true);
272 | //
273 | // onCancel();
274 | // }
275 | // }
276 | //
277 | // @Override
278 | // public void onCancel() {
279 | // ToastUtil.showToast(context, R.string.share_weibosdk_auth_canceled, true);
280 | // if (null != authListener) {
281 | // authListener.onCancel();
282 | // }
283 | // }
284 | //
285 | // @Override
286 | // public void onWeiboException(WeiboException e) {
287 | // ToastUtil.showToast(context,"Auth exception : " + e.getMessage(), true);
288 | // if (null != authListener) {
289 | // authListener.onWeiboException(e);
290 | // }
291 | // }
292 | // }
293 | //
294 | //}
295 |
--------------------------------------------------------------------------------
/shareLibrary/src/main/java/com/xyzlf/share/library/ShareDialogActivity.java:
--------------------------------------------------------------------------------
1 | package com.xyzlf.share.library;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Build;
6 | import android.os.Bundle;
7 | import android.util.SparseArray;
8 | import android.view.MotionEvent;
9 | import android.view.View;
10 | import android.view.ViewConfiguration;
11 | import android.view.ViewGroup;
12 | import android.widget.AdapterView;
13 | import android.widget.BaseAdapter;
14 | import android.widget.GridView;
15 | import android.widget.ImageView;
16 | import android.widget.TextView;
17 |
18 | import com.xyzlf.share.library.bean.ChannelEntity;
19 | import com.xyzlf.share.library.bean.ShareEntity;
20 | import com.xyzlf.share.library.interfaces.ShareConstant;
21 | import com.xyzlf.share.library.util.ChannelUtil;
22 | import com.xyzlf.share.library.util.ShareUtil;
23 | import com.xyzlf.share.library.util.ToastUtil;
24 |
25 | import java.util.ArrayList;
26 | import java.util.List;
27 |
28 |
29 | public class ShareDialogActivity extends ShareBaseActivity implements AdapterView.OnItemClickListener {
30 |
31 | protected List channelEntities;
32 |
33 | protected ShareEntity data;
34 | protected SparseArray sparseArray;
35 |
36 | @Override
37 | protected void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 | setContentView(R.layout.share_activity_dialog);
40 |
41 | Object object = null;
42 | if (getIntent().hasExtra(ShareConstant.EXTRA_SHARE_DATA)) {
43 | Bundle bundle = null;
44 | try {
45 | bundle = getIntent().getBundleExtra(ShareConstant.EXTRA_SHARE_DATA);
46 | } catch (Exception ignore) {}
47 |
48 | if (null != bundle) {
49 | object = bundle.get(ShareConstant.EXTRA_SHARE_DATA);
50 | } else {
51 | try {
52 | object = getIntent().getParcelableExtra(ShareConstant.EXTRA_SHARE_DATA);
53 | } catch (Exception ignore) {}
54 | if (null == object) {
55 | object = getIntent().getSerializableExtra(ShareConstant.EXTRA_SHARE_DATA);
56 | }
57 | }
58 | } else {
59 | object = getIntent().getData();
60 | }
61 |
62 | if (null == object) {
63 | ToastUtil.showToast(this, getString(R.string.share_empty_tip), true);
64 | finish();
65 | return;
66 | }
67 |
68 | if (object instanceof ShareEntity) {
69 | data = (ShareEntity) object;
70 | } else if (object instanceof SparseArray) {
71 | sparseArray = (SparseArray) object;
72 | }
73 | if (data == null && sparseArray == null) {
74 | ToastUtil.showToast(this, getString(R.string.share_empty_tip), true);
75 | finish();
76 | return;
77 | }
78 |
79 | initChannelData();
80 | if (channelEntities.isEmpty()) {
81 | finish();
82 | return;
83 | }
84 | initView();
85 | }
86 |
87 | private void initChannelData() {
88 | channelEntities = new ArrayList<>();
89 | /** weixin **/
90 | if (ChannelUtil.isWeixinInstall(this)) {
91 | if ((channel & ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND) > 0 && isShowChannel(ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND)) {
92 | channelEntities.add(new ChannelEntity(ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND, R.drawable.share_wechat, getString(R.string.share_channel_weixin_friend)));
93 | }
94 | if ((channel & ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE) > 0 && isShowChannel(ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE)) {
95 | channelEntities.add(new ChannelEntity(ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE, R.drawable.share_wxcircle, getString(R.string.share_channel_weixin_circle)));
96 | }
97 | }
98 | /** QQ **/
99 | if (ChannelUtil.isQQInstall(this)) {
100 | if ((channel & ShareConstant.SHARE_CHANNEL_QQ) > 0 && isShowChannel(ShareConstant.SHARE_CHANNEL_QQ)) {
101 | channelEntities.add(new ChannelEntity(ShareConstant.SHARE_CHANNEL_QQ, R.drawable.share_qq, getString(R.string.share_channel_qq)));
102 | }
103 | if ((channel & ShareConstant.SHARE_CHANNEL_QZONE) > 0 && isShowChannel(ShareConstant.SHARE_CHANNEL_QZONE)) {
104 | channelEntities.add(new ChannelEntity(ShareConstant.SHARE_CHANNEL_QZONE, R.drawable.share_qzone, getString(R.string.share_channel_qzone)));
105 | }
106 | }
107 | /** weibo **/
108 | if ((channel & ShareConstant.SHARE_CHANNEL_SINA_WEIBO) > 0 && isShowChannel(ShareConstant.SHARE_CHANNEL_SINA_WEIBO)) {
109 | channelEntities.add(new ChannelEntity(ShareConstant.SHARE_CHANNEL_SINA_WEIBO, R.drawable.share_weibo, getString(R.string.share_channel_weibo)));
110 | }
111 | /** more **/
112 | if ((channel & ShareConstant.SHARE_CHANNEL_SYSTEM) > 0 && isShowChannel(ShareConstant.SHARE_CHANNEL_SYSTEM)) {
113 | channelEntities.add(new ChannelEntity(ShareConstant.SHARE_CHANNEL_SYSTEM, R.drawable.share_more, getString(R.string.share_channel_more)));
114 | }
115 | }
116 |
117 | private boolean isShowChannel(int channel) {
118 | if (sparseArray != null) {
119 | if (sparseArray.get(channel) != null) {
120 | return true;
121 | }
122 | return false;
123 | }
124 | return true;
125 | }
126 |
127 | private void initView() {
128 | AppGridAdapter adapter = new AppGridAdapter();
129 | GridView shareGridView = (GridView) findViewById(R.id.share_grid);
130 | shareGridView.setAdapter(adapter);
131 | shareGridView.setOnItemClickListener(this);
132 | }
133 |
134 | @Override
135 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
136 | super.onActivityResult(requestCode, resultCode, data);
137 | if (requestCode == ShareConstant.REQUEST_CODE) {
138 | if (null != data) {
139 | int channel = data.getIntExtra(ShareConstant.EXTRA_SHARE_CHANNEL, -1);
140 | int status = data.getIntExtra(ShareConstant.EXTRA_SHARE_STATUS, -1);
141 | finishWithResult(channel, status);
142 | return;
143 | }
144 | }
145 | finish();
146 | }
147 |
148 | @Override
149 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
150 | ChannelEntity data = ((ChannelEntity) parent.getAdapter().getItem(position));
151 | if (null == data) {
152 | return;
153 | }
154 | handleShare(data.getchannel());
155 | }
156 | /**
157 | * 分享
158 | * @param channel {@link ShareConstant#SHARE_CHANNEL_ALL}
159 | */
160 | protected void handleShare(int channel) {
161 | switch (channel) {
162 | case ShareConstant.SHARE_CHANNEL_QQ:
163 | shareByQQ();
164 | break;
165 | case ShareConstant.SHARE_CHANNEL_QZONE:
166 | shareByQZone();
167 | break;
168 |
169 | case ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND:
170 | shareByWeixinFriend();
171 | break;
172 | case ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE:
173 | shareByWeixinCircle();
174 | break;
175 |
176 | case ShareConstant.SHARE_CHANNEL_SINA_WEIBO:
177 | shareBySinaWeibo();
178 | break;
179 |
180 | case ShareConstant.SHARE_CHANNEL_SYSTEM:
181 | shareBySystem();
182 | finish();
183 | break;
184 |
185 | case ShareConstant.SHARE_CHANNEL_SMS:
186 | shareBySms();
187 | break;
188 |
189 | case ShareConstant.SHARE_CHANNEL_EMAIL:
190 | shareByEmail();
191 | break;
192 | }
193 | }
194 | /**
195 | * 分享QQ好友
196 | */
197 | protected void shareByQQ() {
198 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_QQ, getShareData(ShareConstant.SHARE_CHANNEL_QQ), ShareConstant.REQUEST_CODE);
199 | }
200 | /**
201 | * 分享到QQ空间
202 | */
203 | protected void shareByQZone() {
204 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_QZONE, getShareData(ShareConstant.SHARE_CHANNEL_QZONE), ShareConstant.REQUEST_CODE);
205 | }
206 | /**
207 | * 分享微信好友
208 | */
209 | protected void shareByWeixinFriend() {
210 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND, getShareData(ShareConstant.SHARE_CHANNEL_WEIXIN_FRIEND), ShareConstant.REQUEST_CODE);
211 | }
212 | /**
213 | * share to weixin circle
214 | */
215 | protected void shareByWeixinCircle() {
216 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE, getShareData(ShareConstant.SHARE_CHANNEL_WEIXIN_CIRCLE), ShareConstant.REQUEST_CODE);
217 | }
218 | /**
219 | * share more
220 | */
221 | protected void shareBySystem() {
222 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_SYSTEM, getShareData(ShareConstant.SHARE_CHANNEL_SYSTEM), ShareConstant.REQUEST_CODE);
223 | }
224 | /**
225 | * share sms
226 | */
227 | protected void shareBySms() {
228 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_SMS, getShareData(ShareConstant.SHARE_CHANNEL_SMS), ShareConstant.REQUEST_CODE);
229 | }
230 | /**
231 | * share email
232 | */
233 | protected void shareByEmail() {
234 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_EMAIL, getShareData(ShareConstant.SHARE_CHANNEL_EMAIL), ShareConstant.REQUEST_CODE);
235 | }
236 | /**
237 | * share weibo
238 | */
239 | protected void shareBySinaWeibo() {
240 | ShareUtil.startShare(this, ShareConstant.SHARE_CHANNEL_SINA_WEIBO, getShareData(ShareConstant.SHARE_CHANNEL_SINA_WEIBO), ShareConstant.REQUEST_CODE);
241 | }
242 |
243 | protected ShareEntity getShareData(int shareChannel) {
244 | if (data != null) {
245 | return data;
246 | }
247 | if (sparseArray != null) {
248 | return sparseArray.get(shareChannel);
249 | }
250 | return null;
251 | }
252 |
253 | private class AppGridAdapter extends BaseAdapter {
254 |
255 | @Override
256 | public int getCount() {
257 | return channelEntities.size();
258 | }
259 |
260 | @Override
261 | public ChannelEntity getItem(int position) {
262 | return channelEntities.get(position);
263 | }
264 |
265 | @Override
266 | public long getItemId(int position) {
267 | return position;
268 | }
269 |
270 | @Override
271 | public View getView(int position, android.view.View convertView, ViewGroup parent) {
272 | ViewHolder holder;
273 | if (convertView == null) {
274 | holder = new ViewHolder();
275 | convertView = getLayoutInflater().inflate(R.layout.share_gridview_item, parent, false);
276 | holder.textView = (TextView) convertView.findViewById(R.id.text);
277 | holder.imageView = (ImageView) convertView.findViewById(R.id.image);
278 | convertView.setTag(holder);
279 | } else {
280 | holder = (ViewHolder) convertView.getTag();
281 | }
282 | ChannelEntity entity = getItem(position);
283 | holder.imageView.setImageResource(entity.getIcon());
284 | holder.textView.setText(entity.getName());
285 | return convertView;
286 | }
287 | }
288 |
289 | private class ViewHolder {
290 | public TextView textView;
291 | public ImageView imageView;
292 | }
293 |
294 | @Override
295 | public boolean onTouchEvent(MotionEvent event) {
296 | //API小于11 点击外部消失
297 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
298 | if (event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(this, event)) {
299 | finish();
300 | return true;
301 | }
302 | }
303 | return super.onTouchEvent(event);
304 | }
305 |
306 | private boolean isOutOfBounds(Activity context, MotionEvent event) {
307 | final int x = (int) event.getX();
308 | final int y = (int) event.getY();
309 | final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
310 | final View decorView = context.getWindow().getDecorView();
311 | return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop)) || (y > (decorView.getHeight() + slop));
312 | }
313 |
314 |
315 | }
316 |
--------------------------------------------------------------------------------