Android实现第三方登录

在前面的文章中有写到用友盟SDK实现分享的功能Android集成友盟社会化分享,今天这篇文章些用友盟SDK实现第三方登录的功能。
配置工程请看分享的这篇文章,这里就不重复讲,因为微信登录需要开发者账号认证需要300大洋,这里就不实现这个功能了,只要你的账号认证过,那么也就是可以用的。
第三方登录也就只需要移动端拿到accessToken和用户的资料传给服务器就行了。
界面布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

    <TextView  android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center" android:text="第三方登录" />

    <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">

        <ImageView  android:id="@+id/qq" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/umeng_socialize_qq_on" />

        <ImageView  android:id="@+id/weixin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/umeng_socialize_wechat" />

        <ImageView  android:id="@+id/sina" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/umeng_socialize_sina_on" />
    </LinearLayout>
    <TextView  android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>

Android实现第三方登录_第1张图片
功能实现代码如下:

package com.ybws.ucast;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;

import java.util.Map;
import java.util.Set;

/** * Created by lzh on 2016/4/5. */
public class ThirdLoginActivity extends Activity implements View.OnClickListener {
    private ImageView qqLogin, weiXinLogin, sinaLogin;
    private UMSocialService mController;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initData();
        initView();
        setListener();
    }

    private void initData() {
        mController = UMServiceFactory.getUMSocialService("com.umeng.login");
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(ThirdLoginActivity.this, "110xxx36",
                "APvxxxsIQS57");
        qqSsoHandler.addToSocialSDK();
        //设置新浪SSO handler
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(ThirdLoginActivity.this, "wxdxxxxb4c0d95", "1ad7f153a54eaxxxxf0f8fe333");
        wxHandler.addToSocialSDK();
    }

    private void initView() {
        qqLogin = (ImageView) findViewById(R.id.qq);
        weiXinLogin = (ImageView) findViewById(R.id.weixin);
        sinaLogin = (ImageView) findViewById(R.id.sina);
        textView = (TextView) findViewById(R.id.message);
    }

    private void setListener() {
        qqLogin.setOnClickListener(this);
        weiXinLogin.setOnClickListener(this);
        sinaLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qq:
                login(SHARE_MEDIA.QQ);
                break;
            case R.id.weixin:
                login(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.sina:
                login(SHARE_MEDIA.SINA);
                break;
        }
    }

    private void login(final SHARE_MEDIA media) {
        mController.doOauthVerify(ThirdLoginActivity.this, media, new SocializeListeners.UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA platform) {
                Toast.makeText(ThirdLoginActivity.this, "授权开始", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(SocializeException e, SHARE_MEDIA platform) {
                Toast.makeText(ThirdLoginActivity.this, "授权错误", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete(Bundle value, SHARE_MEDIA platform) {
                Toast.makeText(ThirdLoginActivity.this, "授权完成", Toast.LENGTH_SHORT).show();
                //获取相关授权信息
                mController.getPlatformInfo(ThirdLoginActivity.this, media, new SocializeListeners.UMDataListener() {
                    @Override
                    public void onStart() {
                        Toast.makeText(ThirdLoginActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete(int status, Map<String, Object> info) {
                        if (status == 200 && info != null) {
                            StringBuilder sb = new StringBuilder();
                            Set<String> keys = info.keySet();
                            for (String key : keys) {
                                sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                textView.setText(sb);
                            }

                        } else {
                            Toast.makeText(ThirdLoginActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }

            @Override
            public void onCancel(SHARE_MEDIA platform) {
                Toast.makeText(ThirdLoginActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

}

Android实现第三方登录_第2张图片
Android实现第三方登录_第3张图片
因为qq appkey和appID的问题,这里暂时拿不到用户的相关信息,所以暂时只能拿到新浪微博的accessToken和用户资料。这样一个简单的第三方登录的功能就能实现了。
如果微信登录出现下面这个界面那么恭喜你,如果要实现微信登录就需要300大洋去认证开发者账号了。
Android实现第三方登录_第4张图片

你可能感兴趣的:(android,sdk,社会化)