ESSocialSDK:Android 社会化授权登录和分享工具

2015-12-15 10:56:31 +08:00
 hailong0707

之前发布过一篇 http://www.v2ex.com/t/238165 ,当时功能不是很完善,文档也不够清楚。
当前发布正式版 0.2.0 ,所有功能经过测试。

Github: https://github.com/ElbbbirdStudio/ESSocialSDK




社交登录授权,分享 SDK

支持微信、微博、 QQ 登录授权

微信好友、微信朋友圈、微博、 QQ 好友、 QQ 空间分享以及系统默认分享

说明

每单个平台全部提供文档说明,内容较多,易混淆集成过程,所以,这里只提供一键登录,一键分享文档,使用默认 UI 。

如果需要单个平台的详细集成文档,参考README_Detail.md

<!-- more -->
默认 UI 效果截图:


Gradle

compile 'com.elbbbird.android:socialsdk:0.2.0@aar'

Debug 模式

SocialSDK.setDebugMode(true); //默认 false

项目配置

package com.encore.actionnow.wxapi;
public class WXEntryActivity extends WXCallbackActivity {

}
<activity
    android:name=".wxapi.WXEntryActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:exported="true"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
    android:name="com.tencent.tauth.AuthActivity"
    android:launchMode="singleTask"
    android:noHistory="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:scheme="tencentXXXXXXXXX" />
    </intent-filter>
</activity>

以上配置中的XXXXXXXXX换成 app_id.

一键登录授权功能(微博,微信, QQ )

授权结果回调

SDK 使用了Otto作为事件库,用以组件通信。
在调用SocialSDK.oauth()接口ActivityonCreate()方法内添加

BusProvider.getInstance().register(this);

在该ActivityonDestroy()方法添加

@Override
protected void onDestroy() {
    BusProvider.getInstance().unregister(this);
    super.onDestroy();
}

添加回调接口

@Subscribe
public void onOauthResult(SSOBusEvent event) {
    switch (event.getType()) {
        case SSOBusEvent.TYPE_GET_TOKEN:
            SocialToken token = event.getToken();
            Log.i(TAG, "onOauthResult#BusEvent.TYPE_GET_TOKEN " + token.toString());
            break;
        case SSOBusEvent.TYPE_GET_USER:
            SocialUser user = event.getUser();
            Log.i(TAG, "onOauthResult#BusEvent.TYPE_GET_USER " + user.toString());
            break;
        case SSOBusEvent.TYPE_FAILURE:
            Exception e = event.getException();
            Log.i(TAG, "onOauthResult#BusEvent.TYPE_FAILURE " + e.toString());
            break;
        case SSOBusEvent.TYPE_CANCEL:
            Log.i(TAG, "onOauthResult#BusEvent.TYPE_CANCEL");
            break;
    }
}

Oauth

SocialSDK.init("wechat_app_id", "wechat_app_secret", "weibo_app_id", "qq_app_id");
SocialSDK.oauth(context);

Revoke

SocialSDK.revoke(context);

一键分享功能(微博,微信,朋友圈, QQ , QQ 空间)

SDK 中SocialShareScene的定义

/**
 * 社会化分享数据类
 */
public class SocialShareScene implements Serializable {

    public static final int SHARE_TYPE_DEFAULT = 0;
    public static final int SHARE_TYPE_WEIBO = 1;
    public static final int SHARE_TYPE_WECHAT = 2;
    public static final int SHARE_TYPE_WECHAT_TIMELINE = 3;
    public static final int SHARE_TYPE_QQ = 4;
    public static final int SHARE_TYPE_QZONE = 5;

    /**
     * @param id        分享唯一标识符,可随意指定,会在分享结果 ShareBusEvent 中返回
     * @param appName   分享到 QQ 时需要指定,会在分享弹窗中显示该字段
     * @param type      分享类型
     * @param title     标题
     * @param desc      简短描述
     * @param thumbnail 缩略图网址
     * @param url       WEB 网址
     */
    public SocialShareScene(int id, String appName, int type, String title, String desc, String thumbnail, String url) {
        this.id = id;
        this.appName = appName;
        this.type = type;
        this.title = title;
        this.desc = desc;
        this.thumbnail = thumbnail;
        this.url = url;
    }

    public SocialShareScene(int id, String appName, String title, String desc, String thumbnail, String url) {
    ....
}

一键分享需要调用第二个构造函数, type 类型在 SDK 内部自动指定

分享结果回调

@Subscribe
public void onShareResult(ShareBusEvent event) {
    switch (event.getType()) {
        case ShareBusEvent.TYPE_SUCCESS:
            Log.i(TAG, "onShareResult#ShareBusEvent.TYPE_SUCCESS " + event.getId());
            break;
        case ShareBusEvent.TYPE_FAILURE:
            Exception e = event.getException();
            Log.i(TAG, "onShareResult#ShareBusEvent.TYPE_FAILURE " + e.toString());
            break;
        case ShareBusEvent.TYPE_CANCEL:
            Log.i(TAG, "onShareResult#ShareBusEvent.TYPE_CANCEL");
            break;
    }
}

ShareTo

SocialSDK.setDebugMode(true);
SocialSDK.init("wechat_app_id", "weibo_app_id", "qq_app_id");
SocialSDK.shareTo(context, scene);

FAQ

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
##微信
-keep class com.tencent.mm.sdk.** {*;}

##微博
-keep public class com.sina.weibo.** {*;}
-keep public class com.sina.sso.** {*;}

##otto
-keepattributes *Annotation*
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}

LICENSE

Copyright 2015 The ESSocialSDK authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2746 次点击
所在节点    程序员
3 条回复
oott123
2015-12-15 12:22:01 +08:00
在 Android 上最痛恨的事情之一莫过于内置分享组件(甚至都没有“更多”给你选)的 App 。
不过社会化登录倒是还好……
anthonyeef
2015-12-15 12:32:39 +08:00
好赞!
hailong0707
2015-12-15 14:09:39 +08:00
@oott123 之所以内置,是因为这样才能分享更好的效果, android 标准接口做不到

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/243630

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX