activity > service 만 통신을 하다가 service > activity 로 결과파일을 받고자 할 때 aidl 을 거쳐서 받는 방법을 서술하겠습니다
Activitiy 는 socketManager 이고
Service 는 CGService 입니다
개요는
.aidl 에 callback interface, startTask 메소드 추가
socketManager 의 ServiceConnection 부분에 binder.startTask 추가
socketManager 에 asBinder, onResult 메소드 추가
CGService 의 ICGService.Stub 에 startTask 추가
정도가 있습니다.
.aidl 에 callback interface, startTask 메소드 추가
// ICGService.aidl
package com.---.test_ga;
// Declare any non-default types here with import statements
import com.---.test_ga.CGMessage;
interface ICGService {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
int getStatus();
void setSocket(String ip);
void connect();
void disconnect();
int sendJsonStr(String jsonStr);
int sendCGMsg(in CGMessage cgmsg);
void receive(int msgId);
void startTask(ICGServiceCallBack callback);
}
interface ICGServiceCallBack {
void onResult(String jsonStr);
}
socketManager 의 ServiceConnection 부분에 binder.startTask 추가
socketManager 에 asBinder, onResult 메소드 추가
public class SocketManager extends Application implements ICGServiceCallBack{
private static final SocketManager instance = new SocketManager();
private static Context context = null;
private ICGService binder = null;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = ICGService.Stub.asInterface(service);
// we use instance, need to set instance's binder
instance.setBinder(binder);
try {
binder.startTask(SocketManager.this);
} catch (RemoteException e) {}
}
@Override
public void onServiceDisconnected(ComponentName name) {
binder = null;
}
};
...
@Override
public IBinder asBinder() {
return null;
}
@Override
public void onResult(String jsonStr) throws RemoteException{
// add result action
}
}
CGService 의 ICGService.Stub 에 startTask 추가
ICGService.Stub binder = new ICGService.Stub() {
@Override
public int getStatus() throws RemoteException {
return status;
}
@Override
public void setSocket(String ip) throws RemoteException {
mySetSocket(ip);
}
@Override
public void connect() throws RemoteException {
myConnect();
}
@Override
public void disconnect() throws RemoteException {
myDisconnect();
}
@Override
public int sendJsonStr(String jsonStr) throws RemoteException {
return mySend(jsonStr);
}
@Override
public int sendCGMsg(CGMessage cgMessage) throws RemoteException {
return mySendCG(cgMessage);
}
@Override
public void receive(int msgId) throws RemoteException {
myReceive(msgId);
}
@Override
public void startTask(ICGServiceCallBack callback) {
callBack = callback;
}
};
이렇게 한다면 service단에서도 activity 단으로 callback 하는 식으로 메시지를 보낼 수 있습니다.
'개발 > Android Studio' 카테고리의 다른 글
[android] aidl, multiprocess 통신 (0) | 2023.10.12 |
---|---|
[Android] [통신#1] activity 간 intent 로 통신하기 (0) | 2023.04.10 |
[Android] Aidl 로 class 옮기기 (0) | 2023.04.04 |
[Android] PackageManager (0) | 2023.03.25 |
[Android] activity 를 intent 로 실행하는법 (startActivity startActivityForResult) (0) | 2023.03.24 |