2
運行代碼後,我遇到start service
問題。Android設置完成啓動服務的監聽程序
我的服務類:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
mApplication.setService(this);
}
現在:
startService(intent); // service run sucess, but after millisecond delay
mApplication.setIsServiceRunning(true);
mApplication.getService().MyMethodAnyThing(); // <--- NullPointerExeption, because in my class mApplication.setService(this) do with delay and getService is null.
我需要completed start service
。例如:
startService(intent);
mApplication.setIsServiceRunning(true);
// i need like listener
@Override
onServiceIsRunComplete() {
// here i'm sure that service is run
mApplication.getService().MyMethodAnyThing();
}
然後使用'bindService'並等待'ServiceConnection' – pskink
這很奇怪,因爲服務與您的活動在同一線程上運行 –
@TimCastelijns是的,對我來說奇怪 – grizzly