天天看點

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實作第三方登入

功能實作代碼如下:

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 ==  && 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實作第三方登入
Android實作第三方登入

因為qq appkey和appID的問題,這裡暫時拿不到使用者的相關資訊,是以暫時隻能拿到新浪微網誌的accessToken和使用者資料。這樣一個簡單的第三方登入的功能就能實作了。

如果微信登入出現下面這個界面那麼恭喜你,如果要實作微信登入就需要300大洋去認證開發者賬号了。

Android實作第三方登入