3
我想通過一個帶有aidl文件的服務綁定2個應用程序。帶有aidl文件的遠程服務:ServiceConnection從未呼叫
在我的應用程序(與將要訪問的服務):
→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」,但方法onServiceConnected in m y serviceConnection永遠不會被調用。我已經設置了一個按鈕來調用服務的方法(參見上面的方法「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
任何想法? 謝謝您的閱讀!
它就像一個魅力!我也在應用程序B中更改了包名,以避免安全錯誤(java.lang.SecurityException:將Binder調用到不正確的接口)。謝謝Lumos〜 – johann 2013-03-11 03:56:32