서비스에서 활동으로의 맞춤형 예외를 버립니다.
문제
저는 현재 Android에서 XMPP 앱을 작업하고 있으며 서비스에서 내 활동에 대한 리모티브 인식과 다른 유형의 예외를 던지는 가장 좋은 방법에 대해 숙고하고 있습니다.
IPC를 사용하여 Remoteexception 이외의 다른 것을 던지는 것은 불가능한 것 같습니다 (.aidl에 아무것도 던질 수 없음). 두 가지 해결책 만 볼 수 있습니다.
내 활동에 대한 청취자를 만들어 내 사용자 정의 XMPP 예외에서 듣는다. 실제로 던지지 않고 소포 프로토콜을 구현하는 일반적인 객체로 전송됩니다.
내 XMPPException을 잡고 XMPPException으로 업데이트 된 컨텐츠를 사용하여 remoteexception을 던지지 만,이 경우 XMPP 또는 실제 remoteexception이라면 내 활동에 대해 어떻게 알 수 있습니까? 예외의 이름을 태그하고 내 활동에 구문 분석해야합니까? 정말 고어 일 것입니다.
아이디어가 있습니까? SDK 문서에서 무언가를 놓쳤습니까?
감사.
다른 팁
Remoteexception에서 파생 된 사용자 정의 예외를 던질 수있는 것 같습니다. 따라서 xmppremoteexception 또는 원래 예외를 유지하는 일반적인 myremoteexception을 가질 수 있습니다. 아래는 두 번째 사례의 데모입니다.
섬기는 사람:
try {
...
}
catch(XMPPException e) {
throw new MyRemoteException(e);
}
고객:
try {
service.someCall();
}
catch(MyRemoteException e) {
rethrow(e);
}
도우미 방법 :
private void rethrow(MyRemoteException e) throws Exception {
if(e.innerException instanceof XMPPException)
throw (XMPPException)e.innerException;
else
throw e.innerException;
}
예외:
public class MyRemoteException extends RemoteException {
private static final long serialVersionUID = 1L;
public Exception innerException;
public MyRemoteException() {}
public MyRemoteException(Exception innerException) {
this.innerException = innerException;
}
}
"서비스에서 활동에 대한 맞춤형 예외를 던지는 것은 불가능하다고 생각합니다. 소포의 자원을 참조하십시오.
/**
* Use this function for customized exception handling.
* customized method call this method for all unknown case
* @param code exception code
* @param msg exception message
*/
public final void readException(int code, String msg) {
switch (code) {
case EX_SECURITY:
throw new SecurityException(msg);
case EX_BAD_PARCELABLE:
throw new BadParcelableException(msg);
case EX_ILLEGAL_ARGUMENT:
throw new IllegalArgumentException(msg);
case EX_NULL_POINTER:
throw new NullPointerException(msg);
case EX_ILLEGAL_STATE:
throw new IllegalStateException(msg);
}
throw new RuntimeException("Unknown exception code: " + code
+ " msg " + msg);
}
따라서 우리는 위의 5 가지 예외를 던질 수 있다는 것을 알 수 있습니다.
예를 들면 : 서비스가 불법적 인 지출을 던지는 경우 :
@Override
public void addImage(final int align, final byte[] imageData) throws RemoteException {
log("/// addImage ///");
if (imageData == null) {
throw new IllegalArgumentException("The second argument(image data) can not be empty!");
}
...
}
고객은 그것을 잡을 수 있습니다 :
try {
printer.addImage(0, null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}