import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.util.Base64;
import android.util.Log;
import com.kakao.auth.ApprovalType;
import com.kakao.auth.AuthType;
import com.kakao.auth.IApplicationConfig;
import com.kakao.auth.ISessionConfig;
import com.kakao.auth.KakaoAdapter;
import com.kakao.auth.KakaoSDK;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import static com.kakao.util.helper.Utility.getPackageInfo;
public class App extends Application {
private class KakaoSDKAdapter extends KakaoAdapter {
@Override
public ISessionConfig getSessionConfig() {
return new ISessionConfig() {
@Override
public AuthType[] getAuthTypes() {
return new AuthType[] {AuthType.KAKAO_LOGIN_ALL};
}
@Override
public boolean isUsingWebviewTimer() {
return false;
}
@Override
public boolean isSecureMode() {
return false;
}
@Override
public ApprovalType getApprovalType() {
return ApprovalType.INDIVIDUAL;
}
@Override
public boolean isSaveFormData() {
return true;
}
};
}
@Override
public IApplicationConfig getApplicationConfig() {
return new IApplicationConfig() {
@Override
public Context getApplicationContext() {
return App.this.getApplicationContext();
}
};
}
}
@Override
public void onCreate() {
super.onCreate();
KakaoSDK.init(new KakaoSDKAdapter());
Log.e("Key Hash : ", getKeyHash(this));
}
public String getKeyHash(final Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_SIGNATURES);
if (packageInfo == null)
return null;
for (Signature signature : packageInfo.signatures) {
try {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
return Base64.encodeToString(md.digest(), Base64.NO_WRAP);
} catch (NoSuchAlgorithmException e) {
Log.e("HHASH", "Unable to get MessageDigest. signature=" + signature, e);
}
}
return null;
}
}
}
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.androidquery.AQuery;
import com.kakao.auth.ISessionCallback;
import com.kakao.auth.Session;
import com.kakao.network.ErrorResult;
import com.kakao.usermgmt.LoginButton;
import com.kakao.usermgmt.UserManagement;
import com.kakao.usermgmt.callback.LogoutResponseCallback;
import com.kakao.usermgmt.callback.MeResponseCallback;
import com.kakao.usermgmt.response.model.UserProfile;
import com.kakao.util.exception.KakaoException;
import com.kakao.util.helper.log.Logger;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity {
private SessionCallback callback;
TextView user_nickname,user_email;
CircleImageView user_img;
LinearLayout success_layout;
Button logout_btn;
LoginButton loginButton;
AQuery aQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aQuery = new AQuery(this);
callback = new SessionCallback();
Session.getCurrentSession().addCallback(callback);
// 카카오톡 로그인 버튼
loginButton = (LoginButton)findViewById(R.id.com_kakao_login);
loginButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(!isConnected()){
Toast.makeText(MainActivity.this,"인터넷 연결을 확인해주세요",Toast.LENGTH_SHORT).show();
}
}
if(isConnected()){
return false;
}else{
return true;
}
}
});
// 로그인 성공 시 사용할 뷰
success_layout = (LinearLayout)findViewById(R.id.success_layout);
user_nickname =(TextView)findViewById(R.id.user_nickname);
user_img =(CircleImageView) findViewById(R.id.user_img);
user_email =(TextView)findViewById(R.id.user_email);
logout_btn = (Button)findViewById(R.id.logout);
logout_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Session.getCurrentSession().isOpened()) {
requestLogout();
}
}
});
if(Session.getCurrentSession().isOpened()){
requestMe();
}else{
success_layout.setVisibility(View.GONE);
loginButton.setVisibility(View.VISIBLE);
}
}
//인터넷 연결상태 확인
public boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (Session.getCurrentSession().handleActivityResult(requestCode, resultCode, data)) {
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
private class SessionCallback implements ISessionCallback {
@Override
public void onSessionOpened() {
//access token을 성공적으로 발급 받아 valid access token을 가지고 있는 상태
// 일반적으로 로그인 후의 다음 activity로 이동한다. (Intent 사용하면 된다)
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent it3 = new Intent(getApplicationContext(), Main.class);
startActivity(it3);
finish(); // 뒤로가기 버튼 눌러도 다시 초기화면 안뜨게
}
}, 1500); // 1500 = 1.5초 후 메인 엑티비티 도출
if(Session.getCurrentSession().isOpened()){ // 한 번더 세션을 체크해주었습니다.
requestMe();
}
}
@Override
public void onSessionOpenFailed(KakaoException exception) {
if(exception != null) {
Logger.e(exception);
}
}
}
private void requestLogout() {
success_layout.setVisibility(View.GONE);
loginButton.setVisibility(View.VISIBLE);
UserManagement.requestLogout(new LogoutResponseCallback() {
@Override
public void onCompleteLogout() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "로그아웃 성공", Toast.LENGTH_SHORT).show();
}
});
}
});
}
private void requestMe() {
success_layout.setVisibility(View.VISIBLE);
loginButton.setVisibility(View.GONE);
UserManagement.requestMe(new MeResponseCallback() {
@Override
public void onFailure(ErrorResult errorResult) {
Log.e("onFailure", errorResult + "");
}
@Override
public void onSessionClosed(ErrorResult errorResult) {
Log.e("onSessionClosed",errorResult + "");
}
@Override
public void onSuccess(UserProfile userProfile) {
Log.e("onSuccess",userProfile.toString());
user_nickname.setText(userProfile.getNickname());
user_email.setText(userProfile.getEmail());
aQuery.id(user_img).image(userProfile.getThumbnailImagePath());
// <- 프로필 작은 이미지 , userProfile.getProfileImagePath() <- 큰 이미지
}
@Override
public void onNotSignedUp() {
Log.e("onNotSignedUp","onNotSignedUp");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
Session.getCurrentSession().removeCallback(callback);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="@+id/success_layout"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/user_img"
android:layout_width="55dp"
android:layout_height="55dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/user_nickname"
android:text="nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/user_email"
android:text="eamil_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/logout"
android:background="@color/colorAccent"
android:textColor="#fff"
android:text="로그아웃"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="40dp"/>
</LinearLayout>
<com.kakao.usermgmt.LoginButton
android:id="@+id/com_kakao_login"
android:layout_width="300dp"
android:layout_height="70dp"/>
</LinearLayout>