Android 如何延迟服务连接呼叫
我正在实现Android 如何延迟服务连接呼叫,android,service,Android,Service,我正在实现服务,该服务与服务器建立TCP连接,然后允许客户端通过该连接传递消息。客户端通过bindService呼叫连接到服务。因此,在客户端ServiceConnection对象中调用了onServiceConnected。问题是从bindService返回后立即调用了onServiceConnected,但此时我的服务未与服务器建立连接。连接未建立时,我是否可以延迟服务连接上的调用?如果不可能,请为我的案例提出一些好的模式。谢谢。您应该按照以下步骤操作: 服务代码: class MyServ
服务
,该服务与服务器建立TCP连接,然后允许客户端通过该连接传递消息。客户端通过bindService
呼叫连接到服务。因此,在客户端ServiceConnection
对象中调用了onServiceConnected
。问题是从bindService
返回后立即调用了onServiceConnected
,但此时我的服务未与服务器建立连接。连接未建立时,我是否可以延迟服务连接上的调用?如果不可能,请为我的案例提出一些好的模式。谢谢。您应该按照以下步骤操作:
服务代码:
class MyService implements Service {
private boolean mIsConnectionEstablished = false;
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public MyService getService() {
// Return this instance of LocalService so clients can call public
// methods
return MyService.this;
}
}
public interface OnConnectionEstablishedListener {
public void onConnectionEstablished();
}
private OnConnectionEstablishedListener mListener;
@Override
public void onCreate() {
super.onCreate();
new Thread( new Runnable() {
@Override
void run() {
//Connect to the server here
notifyConnectionEstablished();
}
}).start();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private void notifyConnectionEstablished() {
mIsConnectionEstablished = true;
if(mListener != null) {
mListener.onConnectionEstablished();
}
}
public void setOnConnectionEstablishedListener(
OnConnectionEstablishedListener listener) {
mListener = listener
// Already connected to server. Notify immediately.
if(mIsConnectionEstablished) {
mListener.onConnectionEstablished();
}
}
}
活动代码:
class MyActivity extends Activity implements ServiceConnection,
OnConnectionEstablishedListener {
private MyService mService;
private boolean mBound;
@Override
public void onCreate() {
super.onCreate();
//bind the service here
Intent intent = new Intent(this, MyService.class);
bindService(intent, this, BIND_AUTO_CREATE);
}
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
mService.setOnConnectionEstablishedListener(this);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
@Override
public void onConnectionEstablished() {
// At this point the service has been bound and connected to the server
// Do stuff here
// Note: This method is called from a non-UI thread.
}
}
您应该按照以下步骤进行操作:
服务代码:
class MyService implements Service {
private boolean mIsConnectionEstablished = false;
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public MyService getService() {
// Return this instance of LocalService so clients can call public
// methods
return MyService.this;
}
}
public interface OnConnectionEstablishedListener {
public void onConnectionEstablished();
}
private OnConnectionEstablishedListener mListener;
@Override
public void onCreate() {
super.onCreate();
new Thread( new Runnable() {
@Override
void run() {
//Connect to the server here
notifyConnectionEstablished();
}
}).start();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private void notifyConnectionEstablished() {
mIsConnectionEstablished = true;
if(mListener != null) {
mListener.onConnectionEstablished();
}
}
public void setOnConnectionEstablishedListener(
OnConnectionEstablishedListener listener) {
mListener = listener
// Already connected to server. Notify immediately.
if(mIsConnectionEstablished) {
mListener.onConnectionEstablished();
}
}
}
活动代码:
class MyActivity extends Activity implements ServiceConnection,
OnConnectionEstablishedListener {
private MyService mService;
private boolean mBound;
@Override
public void onCreate() {
super.onCreate();
//bind the service here
Intent intent = new Intent(this, MyService.class);
bindService(intent, this, BIND_AUTO_CREATE);
}
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
mService.setOnConnectionEstablishedListener(this);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
@Override
public void onConnectionEstablished() {
// At this point the service has been bound and connected to the server
// Do stuff here
// Note: This method is called from a non-UI thread.
}
}
有两个连接。一个appService,可以而且应该立即连接。另外,服务应该连接到服务器。应该有两个连接。有两个连接。一个appService,可以而且应该立即连接。另外,服务应该连接到服务器。应该有两个连接。您正在服务上保存活动实例。这不是正确的方法。活动比服务更容易死机,所以你可以有一个NPE。如果您在“恢复时”或“活动停止时”删除,则可能是在服务上保存活动实例。这不是正确的方法。活动比服务更容易死机,所以你可以有一个NPE。也许如果你在恢复或停止活动时删除。。。