0
步驟#1。需要實現接口ServiceConnection
:實施接口ServiceConnection
public class ServiceBinding<T extends ParentService<T>> implements ServiceConnection {
public T bindedService;
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// Warning: Type safety: Unchecked cast from IBinder to ChatService<T>.LocalBinder
bindedService = ((T.LocalBinder) service).getService();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
}
步驟#2。然後家長服務將如下:
public abstract class ParentService<T extends ParentService<T>> extends Service {
public class LocalBinder extends Binder {
public T getService() {
return (T) T.this; // Warning: Unchecked cast from ChatService<T> to T
}
};
}
問:就是這樣一個接受的解決方案?
ServiceConnection
該如何正確實施?
如何製作一個通用的綁定實現以避免重複代碼的問題。 – DukeSpontaneous