일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 부산에서 창원
- httpclient post
- workbench
- android loadImage
- laravel
- spring
- 구포에서 삼량진
- ImageLoad
- 자바
- 낙동강 종주
- android imageView zoom
- android ImageLoad
- 삼량진 자전거
- 구포에서 자전거
- android facebook
- image zoom
- spring boot
- 자전거
- 부산에서 창원 자전거
- 안드로이드 이미지 로드
- mysql
- 구조체
- 삼량진
- C언어
- elk stack
- c언어 구조체
- 로드바이크
- NetworkImageView
- volley
- 창원에서 부산
Archives
- Today
- Total
은근한
Android Facebook 사용 본문
제가 만들고 있는 어플에서 가져온거여서 필요 없는 내용도 있습니다만
안드로이드에서 facebook을 연동하실때 사용하시면 됩니다.
https://developers.facebook.com/ 에서 Login 을 누르시면 기본 튜토리얼이 있는데 설정하시면 됩니다 ^^..
메소드 중에서 checkfacebookLogin, statuslog에서 유저 정보를 처리하게 됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | package android.app.login; import com.facebook.FacebookException; import com.facebook.FacebookRequestError; import com.facebook.Request; import com.facebook.Response; import com.facebook.Session; import com.facebook.SessionDefaultAudience; import com.facebook.SessionState; import com.facebook.model.GraphUser; import com.facebook.widget.LoginButton; import com.facebook.widget.LoginButton.OnErrorListener; import android.app.Activity; import android.app.hairstyle.R; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Fb_login extends Activity { private String TAG = "Fb_login"; private String id, name, gender; public static Activity loginAct; // private boolean bProgressLogin = false; private Bundle mSavedInstanceState = null; private Session.StatusCallback statusCallback = new SessionStatusCallback(); private class SessionStatusCallback implements Session.StatusCallback { @Override public void call(Session session, SessionState state, Exception exception) { if (state == SessionState.OPENED || state == SessionState.OPENED_TOKEN_UPDATED) { // login checkFacebookLogin(); statuslog(); } else if (state == SessionState.CLOSED) ;// logout else if (state == SessionState.CLOSED_LOGIN_FAILED) { // a lot of error bProgressLogin = false; } } protected boolean isSessionOpened() { Session session = Session.getActiveSession(); return (session != null && session.isOpened()); } // check facebookLogin에서 퍼미션 권한을 설정합니다. private void checkFacebookLogin() { Session session = Session.getActiveSession(); if (session != null && bProgressLogin) { boolean logined = session.isOpened(); if (logined) { String PERMISSION = "publish_actions"; if (session.getPermissions().contains(PERMISSION)) { bProgressLogin = false; Log.d(TAG, "gggggggggggg"); // login success } else session.requestNewPublishPermissions(new Session.NewPermissionsRequest( Fb_login.this, PERMISSION)); } else { // login 시도 if (!session.isOpened() && !session.isClosed()) session.openForRead(new Session.OpenRequest( Fb_login.this).setCallback(statusCallback) .setDefaultAudience( SessionDefaultAudience.FRIENDS)); else session.openActiveSession(Fb_login.this, true, statusCallback); } } } // statuslog 에서 onCompleted를 실행하게 되면 GraphUser에서 유저정보를 받아 올 수 있습니다. private void statuslog() { final Session session = Session.getActiveSession(); // Get current logged in user information Request meRequest = Request.newMeRequest(session, new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser user, Response response) { name = user.getName().toString(); id = user.getId().toString(); gender = user.getProperty("gender").toString(); SharedPreferences pref_name = getSharedPreferences("pref_name", MODE_PRIVATE); SharedPreferences.Editor editor_name = pref_name.edit(); editor_name.putString("name", name); editor_name.commit(); SharedPreferences pref_id = getSharedPreferences("pref_id", MODE_PRIVATE); SharedPreferences.Editor editor_id = pref_id.edit(); editor_id.putString("id", id); editor_id.commit(); SharedPreferences pref_gender = getSharedPreferences("pref_gender", MODE_PRIVATE); SharedPreferences.Editor editor_gender = pref_gender.edit(); editor_gender.putString("gender", gender); editor_gender.commit(); FacebookRequestError error = response.getError(); if (error != null) { Log.e("error", error.toString()); } else if (session == Session.getActiveSession()) { // user.getInnerJSONObject() 사용 } Intent intent = new Intent(Fb_login.this, Regist.class); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.putExtra("user_name", pref_name.getString("name", "")); intent.putExtra("user_gender",pref_gender.getString("gender", "")); intent.putExtra("id", id); intent.putExtra("name", name); intent.putExtra("gender", gender); startActivity(intent); } }); /* * Bundle params = new Bundle(); params.putString("fields", * "name,picture,locale,gender"); meRequest.setParameters(params); */ meRequest.executeAsync(); } }; @Override protected void onCreate(Bundle savedInstanceState) { if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); } super.onCreate(savedInstanceState); setContentView(R.layout.fb_login); loginAct = Fb_login.this; mSavedInstanceState = savedInstanceState; Session.openActiveSession(Fb_login.this, false, statusCallback); LoginButton authButton = (LoginButton) findViewById(R.id.authButton); authButton.setSessionStatusCallback(statusCallback); authButton.setOnErrorListener(new OnErrorListener() { @Override public void onError(FacebookException error) { Log.i(TAG, "Error" + error.getMessage()); } }); Button loginbtn = (Button) findViewById(R.id.reg_login); loginbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(Fb_login.this, Regist_1.class); startActivity(intent); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Session.getActiveSession().onActivityResult(this, requestCode, // resultCode, data); boolean processed = false; if (Session.getActiveSession() != null) { processed = Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); } if (!processed) { } } public void onStart() { super.onStart(); if (Session.getActiveSession() != null) Session.getActiveSession().addCallback(statusCallback); Log.d("Fb_login", "---------------------------------onstart"); } public void onStop() { super.onStop(); if (Session.getActiveSession() != null) Session.getActiveSession().removeCallback(statusCallback); } protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (Session.getActiveSession() != null) { Session session = Session.getActiveSession(); Session.saveSession(session, outState); } } } |