This series of articles encapsulates the login sharing function of the third party twice and calls the unified interface, simplifying the steps of login sharing on different platforms.

Series of articles

Series one Secondary encapsulation and use of Android SDK Series two source code analysis series three wechat SDK access series four QQ SDK access series five Sina Weibo SDK access

1 introduction

Now several companies in the market have made social sharing components, such as ShareSDK, Umeng and so on. The only drawback is that you have to register with the corresponding service platform. You cannot use the client SDK alone.

This series of articles is open source to achieve socialized login sharing, unified external interface. Will be interpreted from how to use, source code analysis, each platform access.

Making 2 addresses

Start with the open source code and the packaged SDK.

Github.com/tsy12321/So…

Welcome to make functional requirements and suggestions in the issue!

3 Directory Introduction

As shown in figure:

The directory structure

Jar package is used to package the SDK. When needed, social_sdK. jar can be introduced and then used with the required platform SDK. This way you can reduce the size of the SDK and introduce whatever platform is needed. More reasonable.

Directory structure:

  • App/Demo code
  • Social_sdk/SDK module development completed with Gradle makeJAR into JAR package
  • Social_sdk_vxxx. jar SDK jar package directly used. Match the required platform SDK packages.
  • Weixin_sdk/WeChat SDK
  • qq_sdk/ qq sdk
  • Sina_weibo_sdk/Sina Weibo SDK

4 the Demo is introduced

Replace the QQ AppID, WX AppID, and Weibo AppKey of MainActivity in Demo with their own

public class MainActivity extends AppCompatActivity implements IWeiboHandler.Response{...private static final String WX_APPID = "your wx appid";    // Apply the wX appID
    private static final String QQ_APPID = "your qq appid";    // Apply for the QQ appID
    private static final String SINA_WB_APPKEY = "your sina wb appkey";       // Apply for sina Weibo appkey. }Copy the code

Replace the QQ appID in AndroidManifest with your own

<! --qq--> <activity android:name="com.tencent.tauth.AuthActivity" android:noHistory="true" android:launchMode="singleTask"  > <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="tencent1111111" /> <! </intent-filter> </activity>Copy the code

Replace the signature in the builde.gradle file with your app signature.

After modifying the above three parts, you can run the Demo. The following

Demo

5 Functions

The SDK mainly integrates authorized login and sharing functions

5.1 Authorized Login

  1. Wechat authorized login
  2. QQ Authorized Login
  3. Sina Weibo authorized login

5.2 share

5.2.1 Sharing media

  1. The text
  2. The picture
  3. music
  4. video
  5. Web page

5.2.2 Sharing Platform

  1. Wechat session sharing
  2. Share on wechat moments
  3. QQ to share
  4. QQ Space sharing
  5. Share on Sina Weibo

6 Development Instructions

6.1 to prepare

Put social_sdK.jar and the required platform SDK into the project for reference.

AndroidManifest adds the following basic permissions (some different information will be registered for each platform later explained)

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />Copy the code

6.2 Configuring Platform Information

The platform information needs to be configured once in the project entry (or before invoking).

PlatformConfig.setWeixin(WX_APPID);
PlatformConfig.setQQ(QQ_APPID);
PlatformConfig.setSinaWB(SINA_WB_APPKEY);Copy the code

6.3 Interface Usage

Call method The API is used to call the login or share interface, and the platform is distinguished in the parameters. The callback can receive the result of successful cancellation or failure.

Here is an example :(some platforms have special processing that will be explained later in the platform)

Initialize the API:

SocialApi mSocialApi = SocialApi.get(getApplicationContext());Copy the code

Login authorization:

mSocialApi.doOauthVerify(this, PlatformType.WEIXIN, new AuthListener() {
    @Override
    public void onComplete(PlatformType platform_type, Map<String, String> map) {
        Log.i("tsy"."oncomplete:" + map);
    }

    @Override
    public void onError(PlatformType platform_type, String err_msg) {
        Log.i("tsy"."onError:" + err_msg);
    }

    @Override
    public void onCancel(PlatformType platform_type) {
        Log.i("tsy"."onCancel"); }});Copy the code

Sharing:


// More on sharing media later
ShareWebMedia shareMedia = new ShareWebMedia();
shareMedia.setTitle("Web Sharing Test");
shareMedia.setDescription("Web Sharing Test");
shareMedia.setWebPageUrl("http://www.baidu.com");
shareMedia.setThumb(BitmapUtils.readBitMap(getApplicationContext(), R.mipmap.ic_launcher));

mSocialApi.doShare(this, PlatformType.WEIXIN, shareMedia, new ShareListener() {
    @Override
    public void onComplete(PlatformType platform_type) {
        Log.i("tsy"."share onComplete");
    }

    @Override
    public void onError(PlatformType platform_type, String err_msg) {
        Log.i("tsy"."share onError:" + err_msg);
    }

    @Override
    public void onCancel(PlatformType platform_type) {
        Log.i("tsy"."share onCancel"); }});Copy the code

6.4 Sharing media

Now integrated text sharing, picture sharing, music sharing, video sharing, web sharing 5 sharing media. There may be only one of these different platforms.

6.4.1 Text sharing

ShareTextMedia shareMedia = new ShareTextMedia();
shareMedia.setText("Share Text Test");Copy the code

6.4.2 Photo Sharing

ShareImageMedia shareMedia = new ShareImageMedia();
shareMedia.setImage(BitmapUtils.readBitMap(getApplicationContext(), R.mipmap.ic_launcher));Copy the code

6.4.3 Music Sharing

ShareMusicMedia shareMedia = new ShareMusicMedia();
shareMedia.setTitle("Share music Test");
shareMedia.setDescription("Share music Test");
shareMedia.setMusicUrl("http://idg-tangsiyuan.tunnel.nibaguai.com/splash/music.mp3");
shareMedia.setThumb(BitmapUtils.readBitMap(getApplicationContext(), R.mipmap.ic_launcher));Copy the code

6.4.4 Video Sharing

ShareVideoMedia shareMedia = new ShareVideoMedia();
shareMedia.setTitle("Sharing a video test");
shareMedia.setDescription("Sharing a video test");
shareMedia.setVideoUrl("http://idg-tangsiyuan.tunnel.nibaguai.com/splash/music.mp3");
shareMedia.setThumb(BitmapUtils.readBitMap(getApplicationContext(), R.mipmap.ic_launcher));Copy the code

6.4.5 Web sharing

ShareWebMedia shareMedia = new ShareWebMedia();
shareMedia.setTitle("Web Sharing Test");
shareMedia.setDescription("Web Sharing Test");
shareMedia.setWebPageUrl("http://www.baidu.com");
shareMedia.setThumb(BitmapUtils.readBitMap(getApplicationContext(), R.mipmap.ic_launcher));Copy the code

7 Third-party platform access

7.1 WeChat

7.1.1 integration SDK

Put weixin_sdk_v3.1.1.jar in the directory into the project.

7.1.2 configuration

AndroidManifest adds:

<activity
    android:name="com.tsy.sdk.social.weixin.WXCallbackActivity"
    android:configChanges="orientation|keyboardHidden|navigation|screenSize"
    android:exported="true"
    android:launchMode="singleTop"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity-alias
    android:name=".wxapi.WXEntryActivity"
    android:exported="true"
    android:targetActivity="com.tsy.sdk.social.weixin.WXCallbackActivity" />Copy the code

7.1.3 Constant Definition

Set the configuration information:

PlatformConfig.setWeixin(WX_APPID);Copy the code

PlatformType:

Wechat :PlatformType.WEIXIN(can be used for login and wechat reply sharing)

Circle of friends :PlatformType.WEIXIN_CIRCLE(used for wechat moments sharing)

7.1.4 note

Login and sharing using wechat requires signature packaging, and the signature and package name should be consistent with the information entered on wechat platform.

7.2 QQ

7.2.1 integration SDK

Put the qq_MTA-SDK-1.6.2.jar and QQ_sdk_v3.1.1.jar in the directory into the project.

7.2.2 configuration

AndroidManifest adds:

<! --qq--> <activity android:name="com.tencent.tauth.AuthActivity" android:noHistory="true" android:launchMode="singleTask"  > <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=" Tencent "/> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" />Copy the code

7.2.3 Constant Definition

Set the configuration information:

PlatformConfig.setQQ(QQ_APPID);Copy the code

PlatformType:

Wechat: platformType.qq (can be used for login and QQ sharing)

Circle of friends: platformType.qzone (for SHARING qq controls)

7.2.4 note

QQ login requires signature package, and the signature and package name should be consistent with the information entered in QQ open platform.

7.3 Sina Weibo

7.3.1 integration SDK

Put weibosdkCore_3.1.4. jar in the directory into the project.

Place all so files in the project directory app/jniLibs (the same as libs) and add them to Gradle

android { ... // Add all the so libraries to microbloggingsourceSets {
        main {
            jniLibs.srcDirs = ['jniLibs']}}}Copy the code

7.3.2 configuration

AndroidManifest adds:

<activity android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser" android:configChanges="keyboardHidden|orientation"  android:exported="false" android:windowSoftInputMode="adjustResize"></activity>Copy the code

And add it to the AndroidManifest page of the activity that initiates the sharing

<activity android:name="com.tsy.girl.MainActivity"> <! - a Shared page - > < intent - filter > < action android: name = "com. Sina) weibo) SDK. Action. ACTION_SDK_REQ_ACTIVITY" / > < category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>Copy the code

Then add the following code to the Activity that initiates the share (if MainActivity).

Implement the iweiboHandler. Response interface and write in the implementation method:

@Override
public void onResponse(BaseResponse baseResponse) {
    ((SinaWBHandler)mSocialApi.getSSOHandler(PlatformType.SINA_WB)).onResponse(baseResponse);
}Copy the code

Implement onNewIntent and onActivityResult methods:

@Override
protected void onNewIntent(Intent intent) {
    ((SinaWBHandler)mSocialApi.getSSOHandler(PlatformType.SINA_WB)).onNewIntent(intent, this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mSocialApi.onActivityResult(requestCode, resultCode, data);
}Copy the code

Add the following to onCreate:

if(savedInstanceState ! =null) {
    ((SinaWBHandler)mSocialApi.getSSOHandler(PlatformType.SINA_WB)).onNewIntent(getIntent(), this);
}Copy the code

Then initiate authorization or share code as normal.

7.3.3 Constant Definition

Set the configuration information:

PlatformConfig.setSinaWB(SINA_WB_APPKEY);Copy the code

Sections 7.3.4 note

Sina login sharing requires signature packaging, and the signature and package name should be the same as the information entered by Sina platform.

At the end

The entire project is an extensible SDK that welcomes fork’s implementation of its own platform additions, and the library itself continues to add platforms and feature points. For this article, there will be a basic source code analysis of the entire SDK in the next part.