Android/Tip&Tech

AIDL이 뭘까나?

행복한 수지아빠 2010. 11. 3. 21:07
반응형
1. 인터페이스(aidl) 생성
2. 구현클래스 생성
3. 메니페스트에 적고
4. 엑티비티에서 해당 클래스를가져와서
5. 구현된 함수를 사용.

의 순서로 진행됩니다.

사실 엑티비티 레벨이니 서비스 레벨이니 신경안써도 돼는 작은 어플에서는 샤용할일이 없을듯.

1. aidl
package hell.o;
interface IPlusItService {
int add(int a, int b);
}

2. impl class
package hell.o;

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

public class PlusItService extends Service {

@Override
public IBinder onBind(Intent intent) {
if (IPlusItService.class.getName().equals(intent.getAction())) {
return plusItServiceIf;
}
return null;
}

private IPlusItService.Stub plusItServiceIf = new IPlusItService.Stub() {
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
};
}
3. manifest
        <service android:name="PlusItService">
<intent-filter>
<action android:name="hell.o.IPlusItService"></action>
</intent-filter>
</service>
4. class load
    private IPlusItService plusItServiceIf
먼저 서비스 인터페이스를 담을 변수를 선언
	Intent intent = new Intent(IPlusItService.class.getName());
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
plusItServiceIf = IPlusItService.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
plusItServiceIf = null;
}
}, BIND_AUTO_CREATE);
온크리에이트에 넣을 내용. (상식이있는 인간이라면 ServiceConnection을 밖으로 빼겠지만 졸려죽겠는데 그런거없다능.)

5. use
	int sum = plusItServiceIf.add(a, b);
요거 한줄하려고 이고생을!
반응형