Reputation: 2090
i have problem with start service
after running codes.
my service class:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
mApplication.setService(this);
}
now:
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.
i need completed start service
. example:
startService(intent);
mApplication.setIsServiceRunning(true);
// i need like listener
@Override
onServiceIsRunComplete() {
// here i'm sure that service is run
mApplication.getService().MyMethodAnyThing();
}
Upvotes: 2
Views: 1246
Reputation: 3675
Maybe something like this,
protected ServiceConnection mServerConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.d(LOG_TAG, "onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(LOG_TAG, "onServiceDisconnected");
}
}
public void start() {
mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE);
mContext.startService(intent);
}
Also don't forget to unbindService() when stopping the service.
Upvotes: 1