SocialHelper

At present, almost all apps contain third-party login and sharing functions, most of which were realized by ShareSDK or other SDKS before. However, in some cases, indirect integration through third-party SDKS is not desired, so this component comes into play.

There is no code for this component in the demo, only some configuration and use things, because the application application is really troublesome, but it has been tested in the project, so it can be used safely.

The project address

advantages

  • Easy access to third-party login and sharing and its callbacks
  • Understand the implementation process of third-party login or sharing

usage

How to reference, use, and obfuscate

reference

1. Add the following configuration to build.gradle in the root directory

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
        maven { url "https://dl.bintray.com/thelasterstar/maven/"}}}Copy the code

2. Add the following reference to the module you want to use

dependencies {
    ...
    compile 'com. Making. Arvinljw: SocialHelper: 1.0.4'
}
Copy the code

Note: If v7 packages are used in this Module, exclude can be used to remove references to this library to avoid duplication. Gson does the same

compile ('com. Making. Arvinljw: SocialHelper: 1.0.4'){
    exclude group: 'com.android.support'
}
Copy the code

use

1. Configure the parameters accordingly

Androidmanifest.xml configuration

Add permissions

<! --> <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"/>
Copy the code

Add the Activity under Application

<! --> <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="tencentqqAppId"/>
    </intent-filter>
</activity>
<activity
    android:name="com.tencent.connect.common.AssistActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"/ > <! -- END of QQ configuration --> <! --> <activity Android :name=".wxapi.WXEntryActivity"
    android:exported="true"
    android:label="@string/app_name"/ > <! -- end of wechat configuration --> <! -- No need for weibo -->Copy the code

Note: It should be noted that the qqAppId in THE QQ configuration can be replaced by the qqAppId you applied for

Wechat also needs to create WXEntryActivity in the packageName. Wxapi package, the detailed implementation can be viewed here

The main processing is to determine whether to log in or share when its onResp, and notify the library to trigger the callback

2. Get the instance

The first step is to use facade mode, exposing a SocialHelper that can be used to get instances through builder mode, invoke related methods, and configure callbacks.

socialHelper = new SocialHelper.Builder()
        .setQqAppId("qqAppId")
        .setWxAppId("wxAppId")
        .setWxAppSecret("wxAppSecret")
        .setWbAppId("wbAppKey")
        .setWbRedirectUrl("wbRedirectUrl")
        .build();
Copy the code

3. Call related methods

Here provides QQ, Wechat and Weibo corresponding login and sharing methods

  • public void loginQQ(Activity activity, SocialLoginCallback callback)
  • public void loginWX(Activity activity, SocialLoginCallback callback)
  • public void loginWB(Activity activity, SocialLoginCallback callback)
  • public void shareQQ(Activity activity, SocialShareCallback callback, ShareEntity shareInfo)
  • public void shareWX(Activity activity, SocialShareCallback callback, ShareEntity shareInfo)
  • public void shareWB(Activity activity, SocialShareCallback callback, ShareEntity shareInfo)

This is a parameter that can be explained by its name.

A, incoming Activity

The main reason for this is that callbacks are handled in the current Activity, rather than in the Fragment itself, which also saves some work

Note: Of course, when you call login or share in your Fragment, please use the most familiar method to call back to its Activity to handle the related operation

B. Incoming callbacks

There are two types of callbacks: login callbacks and share callbacks

The callback interfaces are as follows:

public interface SocialCallback {
    void socialError(String msg);
}

public interface SocialLoginCallback extends SocialCallback{
    void loginSuccess(ThirdInfoEntity info);
}

public interface SocialShareCallback extends SocialCallback{
    void shareSuccess();
}
Copy the code

Logon and share callback failures are returned with a message;

After successful login, encapsulated third-party information will be returned, including commonly used nicknames, gender, avatar, login platform, more information can be found in it, the detailed meaning is detailed in Lib notes;

There is only one callback for successful sharing, which is easy to prompt.

C, ShareEntity

This parameter is the focus of sharing and corresponds to QQShareEntity, WXShareEntity, and WBShareEntity for QQ, wechat, and Weibo

Each share type can be created through its corresponding static method

  • QQShareEntity

    • CreateImageTextInfo Text information, web page information
    • CreateImageInfo Pure image information
    • CreateMusicInfo Indicates music information
    • CreateAppInfo Application information
    • CreateImageTextInfoToQZone graphic information to the QQ space
  • WXShareEntity

    • CreateTextInfo Text information
    • CreateImageInfo Pure image information
    • CreateMusicInfo Indicates music information
    • CreateVideoInfo Video information
    • CreateWebPageInfo Information about a web page
  • WBShareEntity

    • CreateTextInfo Plain text information
    • CreateImageTextInfo Image information
    • CreateMultiImageInfo Multi-graph information
    • CreateVideoInfo Video information
    • CreateWebInfo Web page information

The specific parameters and comments are clearly commented in the corresponding class, so there is not much expansion here; The parameter with the ParamsRequired annotation represents required.

Note that some parameters that do not need to be passed can be shared successfully, but may affect the shared style. Please debug by yourself.

4. Callback configuration

Configured in the calling Activity

/ / use: Qq login and share callback, Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);if(data ! = null && socialHelper ! = null) {/ / qq share if you choose to stay in qq, through the home button, then enter the app does not have a callback socialHelper. OnActivityResult (requestCode, the resultCode, data); }} @override protected void onNewIntent(Intent Intent) {super.onnewintent (Intent Intent);if (socialHelper != null) {
        socialHelper.onNewIntent(intent);
    }
}
Copy the code

As for the callback configuration of wechat, it is handled in the onResp of WXEntryActivity as mentioned in the beginning.

At this point, the way of use is basically finished, so there are only four configurations, you can use it happily, of course, the premise is that the corresponding APP application has been successful.

Examples of SocialHelper can be encapsulated into singletons using a tool as shown in the Demo.

confusion

#qq-keep class com.tencent.**{*; }# weibo
-keep class com.sina.weibo.sdk.** { *; }
# WeChat
-keep class com.tencent.mm.opensdk.** { *; }
-keep class packageName.wxapi.** { *; }
#Gson-keep class com.google.gson.** {*; } -keep class com.google.**{*; } -keep class sun.misc.Unsafe { *; } -keep class com.google.gson.stream.** { *; } -keep class com.google.gson.examples.android.model.** { *; }#Socialhelper-keep class net.arvin.socialhelper.**{*; }Copy the code

Note: change wechat’s packageName into its own packageName

Three-party document link

QQ platform

WeChat platform

Microblogging platform

feedback

If you have any good suggestions or questions, please give us your comments.

Matters to do

  • Description of parameters and arrangement of corresponding styles
  • Organize the flow chart of three-party login sharing
  • Improve the demo code

License

Copyright 2017 arvinljw

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except inThe 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 toin 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.
Copy the code