3
我有IntentService应该从其他服务通过结合使用参考:IntentService并结合模式
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不是为绑定模式而设计的,但可能存在针对这些任务的通用解决方案?
谢谢!
CommonsWare sir:为什么android会阻止'IntentService'绑定其他组件? – Kushal 2016-09-05 11:42:01
@Kushal:因为'IntentService'意思是短暂的(几秒到大概一分钟左右)。当'onHandleIntent()'完成所有未完成的命令时'IntentService'自动销毁。 – CommonsWare 2016-09-05 11:43:20