I would like to provide a service that can be called by other app. Therefore, I have a service and an aidl. But when I try to have a separate application to bind this service (bindService), it just returns me false which means fail. Here is my code.
PS: context is already the ApplicationContext which obtained by calling getApplicationContext()
Code that try to bind the service
private static ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
sService = XXXService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
sService = null;
}
};
private static synchronized XXXService getService(final Context context) {
if (sService != null) {
return sService;
} else {
intent.setClassName(context.getPackageName(), "com.xxx.someservice");
if (context.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
Log.i(TAG, "can bind");
} else {
Log.i(TAG, "can not bind");
}
return sService;
}
}
AndroidManifest
<service android:name="com.xxx.someservice"
android:process=":main"
android:exported="true"/>
This seems basically right. I think the issue is that intent.setClassName(context.getPackageName(), “com.xxx.someservice”); should be intent.setClassName(“Your.package.name.with.the.service”, “com.xxx.someservice”); . context.getPackageName() returns the current package name, so this would work if you were trying to bind in your own package, but your question makes it seem like you are doing it in a separate package.