0
Facebook android sdk docs에서 제시된 예제는 Activity를 사용하여 사용자를 인증합니다. 동일한 서비스를 서비스에서 수행 할 수 있습니까? 예제 코드는 매우 편리 할 것입니다. 감사합니다.facebook 서비스에서 안드로이드 인증을위한 SDK 예제
Facebook android sdk docs에서 제시된 예제는 Activity를 사용하여 사용자를 인증합니다. 동일한 서비스를 서비스에서 수행 할 수 있습니까? 예제 코드는 매우 편리 할 것입니다. 감사합니다.facebook 서비스에서 안드로이드 인증을위한 SDK 예제
Activity 이외의 다른 곳에서는 Facebook 개체 내에서 권한을 부여하는 것이 불가능합니다.
당신은 어떤 웹 서비스를 통해 액세스 토큰을 얻으려고 그와
Facebook facebook
을 초기화 할 수 있습니다. 그래도 활동 내에서 세션을 승인하고 바인더를 통해 전달할 수 있습니다.
여기가 저의 예입니다.
이
는 서비스public class ClientService extends Service {
//Facebook object to operate from service
private Facebook facebook;
...
private final ClientBinder clientBinter = new ClientBinder();
/** Binder to service */
public class ClientBinder extends Binder {
public ClientService getService() {
return ClientService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return clientBinter;
}
public void setFacebook(Facebook f) {
ClientService.this.facebook = f;
}
public boolean loggedFacebook() {
if (facebook == null)
return false;
return facebook.isSessionValid();
}
...
}
이며,이 활동에서 사용입니다
public class SomeActivity extends Activity{
private ClientService clientService;
private ServiceConnection clientConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
public void onServiceConnected(ComponentName name, IBinder service) {
ClientService.ClientBinder binder = (ClientService.ClientBinder) service;
clientService = binder.getService();
mBound = true;
if (facebook != null)
if (facebook.isSessionValid())
clientService.setFacebook(facebook);
}
};
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, ClientService.class);
bindService(intent, clientConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(clientConnection);
mBound = false;
}
}
.................
//And HERE IS MAGIC
.....
private void logINFacebook() {
if (facebook.isSessionValid())
return;
facebook.authorize(this,
getResources().getStringArray(R.array.facebookPermissions),
new DialogListener() {
public void onFacebookError(FacebookError e) {...}
public void onError(DialogError e) {...}
public void onComplete(Bundle values) {
if (mBound) {
clientService.setFacebook(facebook);
}
//Save to shared preferences(optional)
SharedPreferences.Editor editor = prefs.edit();
editor.putString(FACEBOOK_TOKEN_PREFNAME,
facebook.getAccessToken());
editor.putLong(FACEBOOK_EXPIRES_PREFNAME,
facebook.getAccessExpires());
editor.commit();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
facebook.authorizeCallback(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}