代码:
@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;
}
};
尝试获取用户信息,但服务为空,因为它尚未绑定。解决此问题的最佳方法是什么?我可以在 onServiceConnected 方法中进行 API 调用,但必须有更好的方法。