我想通过带有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());
}
}
当绑定到service(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
有什么想法吗? 谢谢你的阅读!
答案 0 :(得分:3)
更改
Boolean ret = bindService(i, mConnection , Context.BIND_DEBUG_UNBIND );
为:
Boolean ret = bindService(i, mConnection , Context.BIND_AUTO_CREATE );
然后将调用onServiceConnected。