Android uses Umeng to integrate QQ, WeChat, Weibo and other third-party login

foreword

Recently, the project needs to add third-party sharing and login functions. Previously, the third-party sharing and login of other projects have been implemented using ShareSDK. In order to use Umeng's family bucket uniformly, Umeng is also selected for three-party sharing and login. The complete integration and usage process is recorded here.

1. Apply for Umeng Appkey

You can apply directly to the official website of Youmeng. Generally, you don't need to apply by yourself, you can ask the project manager.
(My Demo here directly uses the Appkey provided in the Umeng Demo for convenience, but the package name of the created project should be the same as that of Umeng. The real project needs to be applied separately)

2. Download SDK

Download address: http://dev.umeng.com/social/android/sdk-download

When downloading, choose according to your own needs. Here I choose to choose the simplified version (including common sharing and login functions), and only test WeChat, QQ, and Sina Weibo.
write picture description here

After downloading, unzip it like this:
write picture description here

3. Prepare resources

Use Umeng's integration tool to quickly integrate Umeng's share SDK: double-click Umeng integration tool.jar -> select the platform and IDE to use, as shown in the figure:
write picture description here

Click the OK button, and a folder named umeng_integratetool_result will be generated in the current directory, as shown in the figure:
write picture description here

4. Start integration

1. Paste the above folders into the corresponding folders of the project in turn, as shown in the figure:

write picture description here

2. Copy the debug.keystore file to the app directory of the project (corresponding to the corresponding location in the app build), in order to use the Umeng's signature, as shown in the figure:

write picture description here

3. Add a callback Activity

1. WeChat callback:
In fact, it has been added in the first step of copying. That is, create a wxapi folder in the package name directory, and create a new activity named WXEntryActivity to inherit WXCallbackActivity

2. Callback of QQ and Sina Weibo:
QQ and Sina do not need to add Activity, but need to add the following code to the Activity that uses QQ to share or authorize:
(Note that onActivityResult cannot be implemented in fragment, if you call login or share in fragment , which needs to be implemented in the fragment-dependent Activity)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}

4. Configure the manifest file Android Manifest

1. Add permissions:

    <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"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

2. Add the Activity required in the sdk

<!--微信-->
        <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=".WBShareActivity"
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <!--qq精简版-->
        <activity
            android:name="com.umeng.qq.tencent.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"/>
                <!--这里的scheme是qq分享要用的,100424468为自己申请的appid,真实项目中需要替换-->
                <data android:scheme="tencent100424468"/>
            </intent-filter>
        </activity>
        <activity
            android:name="com.umeng.qq.tencent.AssistActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
        <!--分享编辑页-->
        <activity
            android:name="com.umeng.socialize.editorpage.ShareActivity"
            android:excludeFromRecents="true"
            android:theme="@style/Theme.UMDefault"
            />

3. Add Umeng appkey

        <meta-data
            android:name="UMENG_APPKEY"
            android:value="561cae6ae0f55abd990035bf">
        </meta-data>

5. Configure the third-party appkey

Create a new MyApplication to inherit Application (note: you need to configure the MyApplication in the manifest file), configure the appkey of the third-party platform and initialize the sdk in the MyApplication file. as follows:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        UMShareAPI.get(this);//初始化sdk
        //开启debug模式,方便定位错误,具体错误检查方式可以查看http://dev.umeng.com/social/android/quick-integration的报错必看,正式发布,请关闭该模式
        Config.DEBUG = true;
    }


    //各个平台的配置
    {
        //微信
        PlatformConfig.setWeixin("wxdc1e388c3822c80b", "3baf1193c85774b3fd9d18447d76cab0");
        //新浪微博(第三个参数为回调地址)
        PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad","http://sns.whalecloud.com/sina2/callback");
        //QQ
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
    }
}

6. Login code MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void qqLogin(View view) {
        authorization(SHARE_MEDIA.QQ);
    }

    public void weiXinLogin(View view) {
        authorization(SHARE_MEDIA.WEIXIN);
    }

    public void sinaLogin(View view) {
        authorization(SHARE_MEDIA.SINA);
    }

    //授权
    private void authorization(SHARE_MEDIA share_media) {
        UMShareAPI.get(this).getPlatformInfo(this, share_media, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                Log.d(TAG, "onStart " + "授权开始");
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                Log.d(TAG, "onComplete " + "授权完成");

                //sdk是6.4.4的,但是获取值的时候用的是6.2以前的(access_token)才能获取到值,未知原因
                String uid = map.get("uid");
                String openid = map.get("openid");//微博没有
                String unionid = map.get("unionid");//微博没有
                String access_token = map.get("access_token");
                String refresh_token = map.get("refresh_token");//微信,qq,微博都没有获取到
                String expires_in = map.get("expires_in");
                String name = map.get("name");
                String gender = map.get("gender");
                String iconurl = map.get("iconurl");

                Toast.makeText(getApplicationContext(), "name=" + name + ",gender=" + gender, Toast.LENGTH_SHORT).show();

                //拿到信息去请求登录接口。。。
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                Log.d(TAG, "onError " + "授权失败");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                Log.d(TAG, "onCancel " + "授权取消");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}

The effect is as follows:

write picture description here

Demo download address: https://github.com/wildma/UMengThirdPartyShareLogin

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325170232&siteId=291194637