3

我想通过一个带有aidl文件的服务来绑定2个应用程序。

在我的应用程序 A 中(带有将被访问的服务):

→aidl 文件(BillingInterface.aidl):

package com.A.service;
import android.os.Bundle;

interface BillingInterface {
    Bundle getServiceDetails(String a, String b);
}

Java接口是自动创建的,没有麻烦。

然后,我的服务将被远程:

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;
            };
        }
    }

}

在我的清单文件中:

<service
        android:name=".service.BillingService"
        android:enabled="true"
        android:exported="true"
        android:process=":remote" 

        <intent-filter>
           <action android:name=".service.BillingInterface.aidl" />
        </intent-filter>
/>

在我的应用程序 B 中,我尝试连接到它(我有相同的aidl 文件):

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());
        }

    }

绑定到服务 (bindService) 时,我得到一个布尔值“true”,但我的 serviceConnection 中的 onServiceConnected 方法永远不会被调用。我设置了一个按钮来调用服务的方法(参见上面的方法“onclick”)。如果我点击我会收到以下消息:

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

有任何想法吗 ?感谢您的阅读!

4

1 回答 1

3

改变

  Boolean ret = bindService(i, mConnection , Context.BIND_DEBUG_UNBIND );

到:

Boolean ret = bindService(i, mConnection , Context.BIND_AUTO_CREATE );

然后将调用 onServiceConnected。

于 2013-03-11T03:29:48.757 回答