I`m trying to build and run a service for react native app, and after the service is running i need to call to some of the service methods what means that i need to get an instance of the running service so im trying to use bindservice().
the problem is that i when i`m calling bindservice() the onServiceConnected() and onStartCommand() wont start, on the other hand the onBind() in the service is starting.
all of the answers i`ve tried didnt help, this is part of them:
ServiceConnection.onServiceConnected() never called after binding to started service onServiceConnected never called after bindService method ServiceConnection::onServiceConnected not called even though Context::bindService returns true? OnServiceConnected not getting called
Thanks in advance.
Here is the Service:
public class TestService extends Service {
private LocalBinder mBinder = new LocalBinder();
private Intent serviceIntent;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.i("service", "service starting!!@!@!@ ");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
Intent broadcastIntent = new Intent("com.testapp.service.RestartTest");
sendBroadcast(broadcastIntent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent broadcastIntent = new Intent("com.testapp.service.RestartTest");
sendBroadcast(broadcastIntent);
super.onTaskRemoved(rootIntent);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
TesService getService() {
return TestService.this;
}
}
}
the package for the react native:
public class TestPackage implements ReactPackage {
private ReactApplicationContext applicationContext;
TestService runningService;
boolean mBound = false;
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
runningService = new TestService(reactContext);
Intent serviceIntent = new Intent(reactContext, runningService.getClass());
reactContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
modules.add(runningService.someMoudle());
return modules;
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
runningService = ((LocalBinder) service).getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}
The AndroidManifest.xml
contains
<service
android:name=".TestService">
</service>
Api.java
ApiPackage.java
i had same problem, then i found your question. so, i want to share my solution. it's work for me.
althouth, it's very late to you, I want to help someone.