이번에는 AuthenticationService 를 실제로 구현해 보도록 하겠습니다.

 

이전에 “Accounts & Sync” 메뉴에 내 계정을 등록시키는 작업을 했습니다.

그 다음에 원하는 것은 내가 만든 계정을 클릭했을 때 로그인 창을 띄우는 것 입니다.

 

그런데 여기서 문제는 실제로 로그인 을 요청하는 부분은 다른 Process 가 한다는 것이죠.

결국 이 프로세스가 우리가 만든 Service 와 통신하기 위해서는 바인더를 이용하여야 합니다.

 

안드로이드에서는 이를 위해서 AbstractAccountAuthenticator 라는 추상 클래스를 제공합니다.

우리는 이를 구현해서 iBinder 만 넘겨주면 됩니다.

 

이를 소스로 살펴보면 아래와 같습니다.

 

package net.cranix.android.cranixsyncsample.authenticator;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class AuthenticationService extends Service {
private Authenticator authenticator;

@Override
public void onCreate() {
authenticator = new Authenticator(this);
}

@Override
public IBinder onBind(Intent intent) {
return authenticator.getIBinder();
}
}



 

즉 Authenticator 는 AbstractAccountAuthenticator 를 구현한 클래스이고 이것을 우리가 만든 서비스에서 만들고 사용하는 모습입니다.

 

이제 로그인 및 기타 처리는 Authenticator 클래스에서 구현해주면 됩니다.

Authenticator 클래스는 아래와 같습니다.

 

package net.cranix.android.cranixsyncsample.authenticator;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.os.Bundle;


public class Authenticator extends AbstractAccountAuthenticator {

public Authenticator(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
public Bundle addAccount(AccountAuthenticatorResponse response,
String accountType, String authTokenType,
String[] requiredFeatures, Bundle options)
throws NetworkErrorException {
// TODO Auto-generated method stub
return null;
}

@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response,
Account account, Bundle options) throws NetworkErrorException {
// TODO Auto-generated method stub
return null;
}

@Override
public Bundle editProperties(AccountAuthenticatorResponse response,
String accountType) {
// TODO Auto-generated method stub
return null;
}

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response,
Account account, String authTokenType, Bundle options)
throws NetworkErrorException {
// TODO Auto-generated method stub
return null;
}

@Override
public String getAuthTokenLabel(String authTokenType) {
// TODO Auto-generated method stub
return null;
}

@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response,
Account account, String[] features) throws NetworkErrorException {
// TODO Auto-generated method stub
return null;
}

@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response,
Account account, String authTokenType, Bundle options)
throws NetworkErrorException {
// TODO Auto-generated method stub
return null;
}

}



 

AbstractAccountAuthenticator 를 상속받은 Authenticator 클래스의 기본형은 위와 같습니다.

이제 각각의 메소드들은 시스템에서 계정관련 작업시 알아서 호출하게 됩니다.

 

여기서는 간단하게 계정추가를 만들어보도록 하겠습니다.

계정추가는 설정에 “Accounts & Sync” 에서 계정을 누를때 일어나며, 위의 메소드 중에서는 “addAccount” 를 호출하게 됩니다.

 

그럼 위 소스를 아래와 같이 수정합니다.

 

public class Authenticator extends AbstractAccountAuthenticator { 
private Context context;
public Authenticator(Context context) {
super(context);
this.context = context;
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response,
String accountType, String authTokenType,
String[] requiredFeatures, Bundle options)
throws NetworkErrorException {

AccountManager manager = AccountManager.get(context);

Account account = new Account("cranix",accountType);
manager.addAccountExplicitly(account, "passowrd", null);
return new Bundle();
}




}

 

여기서는 AccountManager.addAccountExplicitly 를 사용하여 계정을 추가하는데 이것을 사용하려면 권한이 필요합니다.

아래내용을 manifest 파일에 추가합니다.

 

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>

 

이제 설정 메뉴의 “Accounts & Sync” 에서 “Add account” 를 누른후에 자신이 추가한 계정을 클릭하면 아래와 같이 추가되는것을 볼 수 있습니다.

image

 

이번 포스트에서는 UI 없이 간단하게 추가되도록 만들었습니다.

안드로이드에서는 여기서 사용하는 UI 를 지원하기 위해서 AccountAuthenticatorActivity 라는 클래스를 제공합니다.

 

다음 에는 이 클래스를 이용해서 UI 를 붙혀서 로그인 하는법을 알아보도록 하겠습니다.

by cranix 2010. 12. 23. 17:21