Remote service with aidl file : ServiceConnection never called
aidl, android
Solution
change
Boolean ret = bindService(i, mConnection , Context.BIND_DEBUG_UNBIND );
to:
Boolean ret = bindService(i, mConnection , Context.BIND_AUTO_CREATE );
then onServiceConnected will be called.
Problem
I 'd like to bind 2 applications through a service with aidl file. In my application A (with service that will be accessed) : →aidl file (BillingInterface.aidl): ``` package com.A.service; import android.os.Bundle; interface BillingInterface { Bundle getServiceDetails(String a, String b); } ``` Java interface is automatically created without troubles. Then, my service that will be remoted: ``` public class BillingService extends Service { private static final String TAG = BillingService.class.getName(); @Override public void onCreate() { super.onCreate(); } @Override public void onDestroy() { super.onDestroy(); } @Override public IBinder onBind(Intent arg0) { return new BillingInterface.Stub() { @Override public Bundle getServiceDetails(String a, String b) throws RemoteException { Bundle b = new Bundle(); b.putString("test", "simple test"); return b; }; } } } ``` In my manifest file : ``` <service android:name=".service.BillingService" android:enabled="true" android:exported="true" android:process=":remote" <intent-filter> <action android:name=".service.BillingInterface.aidl" /> </intent-filter> /> ``` In my application B, I try to connect to it (I have the same aidl file): ``` protected void onCreate(Bundle savedInstanceState) { ... Intent i = new Intent(); i.setClassName("com.A", "com.A.service.BillingService"); try { Boolean ret = bindService(i, mConnection , Context.BIND_DEBUG_UNBIND ); Log.d("DEBUG", ret.toString()); // will return "true" } catch (Exception e) { Log.e("DEBUG", "not able to bind ! "); } ... private ServiceConnection mConnection = new ServiceConnection(){ public void onServiceConnected(ComponentName name, IBinder boundService) { service = BillingInterface.Stub.asInterface((IBinder) boundService); Log.d("DEBUG", "onServiceConnected() : OK "); } public void onServiceDisconnected(ComponentName name) { service = null; } }; //When clicking button : public void onclick(View v) { try { // serviceのcheckTransfertメッソードを呼ぶ Bundle response = service.getServiceDetails("test", "test"); Log.d("DEBUG", response.getString("test")); } catch (RemoteException e) { Log.e("DEBUG", "error in RemoteExpcetion" + e.getMessage()); } } ``` When binding to service (bindService), I get a boolean "true", but method onServiceConnected in my serviceConnection is never called. I have set a button in order to call service's method (see above method "onclick"). If I click i get the below message : ``` 03-11 02:00:32.895: E/AndroidRuntime(3066): FATAL EXCEPTION: main 03-11 02:00:32.895: E/AndroidRuntime(3066): java.lang.IllegalStateException: Could not execute method of the activity 03-11 02:00:32.895: E/AndroidRuntime(3066): at android.view.View$1.onClick(View.java:2072) 03-11 02:00:32.895: E/AndroidRuntime(3066): at android.view.View.performClick(View.java:2408) 03-11 02:00:32.895: E/AndroidRuntime(3066): at android.view.View$PerformClick.run(View.java:8816) 03-11 02:00:32.895: E/AndroidRuntime(3066): at android.os.Handler.handleCallback(Handler.java:587) 03-11 02:00:32.895: E/AndroidRuntime(3066): at android.os.Handler.dispatchMessage(Handler.java:92) 03-11 02:00:32.895: E/AndroidRuntime(3066): at android.os.Looper.loop(Looper.java:123) 03-11 02:00:32.895: E/AndroidRuntime(3066): at android.app.ActivityThread.main(ActivityThread.java:4627) 03-11 02:00:32.895: E/AndroidRuntime(3066): at java.lang.reflect.Method.invokeNative(Native Method) 03-11 02:00:32.895: E/AndroidRuntime(3066): at java.lang.reflect.Method.invoke(Method.java:521) 03-11 02:00:32.895: E/AndroidRuntime(3066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 03-11 02:00:32.895: E/AndroidRuntime(3066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 03-11 02:00:32.895: E/AndroidRuntime(3066): at dalvik.system.NativeStart.main(Native Method) 03-11 02:00:32.895: E/AndroidRuntime(3066): Caused by: java.lang.reflect.InvocationTargetException 03-11 02:00:32.895: E/AndroidRuntime(3066): at com.example.testconnection/xxxx.MainActivity.onclick(MainActivity.java:59) 03-11 02:00:32.895: E/AndroidRuntime(3066): at java.lang.reflect.Method.invokeNative(Native Method) 03-11 02:00:32.895: E/AndroidRuntime(3066): at java.lang.reflect.Method.invoke(Method.java:521) 03-11 02:00:32.895: E/AndroidRuntime(3066): at android.view.View$1.onClick(View.java:2067) 03-11 02:00:32.895: E/AndroidRuntime(3066): ... 11 more 03-11 02:00:32.895: E/AndroidRuntime(3066): Caused by: java.lang.NullPointerException 03-11 02:00:32.895: E/AndroidRuntime(3066): ... 15 more ``` Any ideas ? Thank you for reading !