0
코드는 :그래서 나는 바운드 서비스를 가지고 있으며, onResume 활동에서 서비스를 사용할 수있는 방법이 있습니까?
@Override
public void onResume() {
super.onResume();
Intent intent = new Intent(getActivity(), UserAPIService.class);
getActivity().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
mService.fetchUserInfo();
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
UserAPIService.LocalBinder binder = (UserAPIService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
사용자 정보를 가져올 수 시도하고 있지만 아직 구속되지 않았기 때문에 서비스가 null입니다. 이 문제를 해결하는 가장 좋은 방법은 무엇입니까? onServiceConnected 메소드에서 API 호출을 만들 수 있지만 더 나은 방법이 있어야합니다.
api 서비스의 응용 프로그램 클래스에서 싱글 톤을 만들면 어떻게됩니까? – Nick
@Nick : 서비스가 바인딩에 의존하는 경우 구성 변경으로 인해 '작업'이 다시 시작될 때 서비스가 삭제되는 것을 방지하는 것이 좋습니다. 이 경우 바인딩 라이프 사이클을 적절하게 관리해야합니다. – corsair992