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