Android IntentService和绑定模式
我有一个IntentService,它应该通过绑定使用来自另一个服务的引用:Android IntentService和绑定模式,android,Android,我有一个IntentService,它应该通过绑定使用来自另一个服务的引用: public class BaseIntentService extends IntentService implements ServiceConnection { protected NetworkApi network; public BaseIntentService() { super("BaseIntentService"); } @Override
public class BaseIntentService extends IntentService implements ServiceConnection {
protected NetworkApi network;
public BaseIntentService() {
super("BaseIntentService");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
network = ((NetworkApiBinder) service).getApi();
// never be invoked
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onCreate() {
super.onCreate();
bindService(new Intent(this, NetworkApi.impl), this, BIND_AUTO_CREATE);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(this);
}
@Override
protected void onHandleIntent(Intent intent) {
// network always null!!!
}
}
但当我使用像这样的绑定时,永远不会调用onServiceConnected。我知道IntentService不是为绑定模式而设计的,但是对于这样的任务可能有一个通用的解决方案吗
谢谢
但当我使用像这样的绑定时,永远不会调用onServiceConnected
这是因为您的IntentService
在绑定请求开始之前就被销毁了。当onHandleIntent()
完成所有未完成的命令时,IntentService
将自动销毁
但是对于这些任务可能有一个共同的解决方案
没有两种服务。摆脱
IntentService
并将其逻辑转移到其他服务中。commonware先生:为什么安卓系统阻止IntentService
与其他组件绑定?@Kushal:因为IntentService
的寿命很短(几秒钟到一分钟左右)。当onHandleIntent()
完成所有未完成的命令时,IntentService
将自动销毁。