2017년 2월 7일 화요일

[Android] 카카오로그인(Kakao Login)시 별도(Custom) 로그인버튼(Button) 사용하기

1. 카카오 로그인 버튼 클래스를 상속받는 로그인 컨트롤 클래스 생성
------------------------------------------------------------------------------------
package com.neutti;

import android.app.Activity;
import android.content.Context;

import com.kakao.auth.AuthType;
import com.kakao.auth.Session;
import com.kakao.usermgmt.LoginButton;

import java.lang.reflect.Method;
import java.util.List;

public class KakaoLoginControl extends LoginButton {
    public KakaoLoginControl(Context context) {
        super(context);
    }

    public void call() {
        try {
            Method methodGetAuthTypes = LoginButton.class.getDeclaredMethod("getAuthTypes");
            methodGetAuthTypes.setAccessible(true);
            final List<AuthType> authTypes = (List<AuthType>) methodGetAuthTypes.invoke(this);
            if(authTypes.size() == 1){
                Session.getCurrentSession().open(authTypes.get(0), (Activity) getContext());
            } else {
                Method methodOnClickLoginButton = LoginButton.class.getDeclaredMethod("onClickLoginButton",List.class);
                methodOnClickLoginButton.setAccessible(true);
                methodOnClickLoginButton.invoke(this,authTypes);
            }
        } catch (Exception e) {
            Session.getCurrentSession().open(AuthType.KAKAO_ACCOUNT, (Activity) getContext());
        }
    }

}
------------------------------------------------------------------------------------
2. 커스텀 버튼(btn_custom_kakao_login) 클릭 이벤트에  컨트롤(KakaoLoginControl) 연결
------------------------------------------------------------------------------------
findViewById(R.id.btn_kakao).setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View view) {
        new KakaoLoginControl([해당 Activity]).call();    }
});
------------------------------------------------------------------------------------


음.. 그리고 버튼 디자인은 최대한 카카오 디자인을 유지해야된다고 함..

KakaoDevelopers 관련답변 : https://devtalk.kakao.com/t/topic/7553
카카오로그인 버튼 가이드 : https://developers.kakao.com/buttons

테스트버전 : android lib - 1.1.33 API