MessengerDEMOActivity.java
public class MessengerDEMOActivity extends Activity { /** Called when the activity is first created. */ private MessengerDEMOServiceConnection mdsconnection = null; private Messenger mMessenger = null; class IncomingHandler extends Handler { public voID handleMessage(Message msg) { Bundle b = msg.getData(); System.out.println("MESSENGER! " + b.getString("MESSENGER")); } } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); mMessenger = new Messenger(new IncomingHandler()); mdsconnection = new MessengerDEMOServiceConnection(mMessenger); Intent intent = new Intent(); intent.putExtra("ID","AIDL"); intent.setClassname("messenger.demo",messenger.demo.MessengerDEMOService.class.getname()); bindService(intent,mdsconnection,Context.BIND_auto_CREATE); }}
MessengerDEMOServiceConnection.java
public class MessengerDEMOServiceConnection implements ServiceConnection { private IMessengerDEMOService service = null; private Messenger mMessenger = null; public MessengerDEMOServiceConnection(Messenger mMessenger) { super(); System.out.println("MessengerDEMOServiceConnection SocketServiceConnection()"); this.mMessenger = mMessenger; } public voID onServiceConnected(Componentname name,IBinder boundService) { System.out.println("MessengerDEMOServiceConnection onServiceConnected()"); service = IMessengerDEMOService.Stub.asInterface((IBinder) boundService); Messenger mService = new Messenger(boundService); Message msg = Message.obtain(null,5); msg.replyTo = mMessenger; try { mService.send(msg); // line 31 } catch (remoteexception e1) { // Todo auto-generated catch block e1.printstacktrace(); } try { service.foo(); } catch (remoteexception e) { // Todo auto-generated catch block e.printstacktrace(); } } public voID onServicedisconnected(Componentname name) { System.out.println("MessengerDEMOServiceConnection onServicedisconnected()"); service = null; }}
MessengerDEMOService.java
public class MessengerDEMOService extends Service { private Messenger mMessenger = null; class IncomingHandler extends Handler { @OverrIDe public voID handleMessage(Message msg) { System.out.println("MessengerDEMOService IncomingHandler"); Messenger activityMessenger = msg.replyTo; Message m = new Message(); Bundle b = new Bundle(); b.putString("MESSENGER","blablabla"); m.setData(b); try { activityMessenger.send(m); } catch (remoteexception e) { // Todo auto-generated catch block e.printstacktrace(); } } } public voID onCreate() { System.out.println("MessengerDEMOService onCreate"); mMessenger = new Messenger(new IncomingHandler()); } public IBinder onBind(Intent intent) { System.out.println("MessengerDEMOService onBind()"); if (intent.getExtras().getString("ID").equals("AIDL") == true) { System.out.println("MessengerDEMOService onBind() AIDL"); return new IMessengerDEMOService.Stub() { public voID foo() { System.out.println("MessengerDEMOService onBind() foo()"); } }; } System.out.println("MessengerDEMOService onBind() MESSENGER"); return mMessenger.getBinder(); }}
并且堆栈跟踪:
12-27 16:42:29.412: INFO/System.out(832): MessengerDEMOServiceConnection SocketServiceConnection()12-27 16:42:29.442: INFO/System.out(832): MessengerDEMOService onCreate12-27 16:42:29.452: INFO/System.out(832): MessengerDEMOService onBind()12-27 16:42:29.452: INFO/System.out(832): MessengerDEMOService onBind() AIDL12-27 16:42:29.542: INFO/System.out(832): MessengerDEMOServiceConnection onServiceConnected()12-27 16:42:29.572: WARN/Parcel(832): **** enforceInterface() expected 'messenger.demo.IMessengerDEMOService' but read 'androID.os.IMessenger'12-27 16:42:29.572: DEBUG/AndroIDRuntime(832): Shutting down VM12-27 16:42:29.572: WARN/dalvikvm(832): threadID=1: thread exiting with uncaught exception (group=0x4001d800)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): FATAL EXCEPTION: main12-27 16:42:29.592: ERROR/AndroIDRuntime(832): java.lang.SecurityException: Binder invocation to an incorrect interface12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.os.Parcel.enforceInterface(Native Method)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at messenger.demo.IMessengerDEMOService$Stub.onTransact(IMessengerDEMOService.java:49)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.os.Binder.transact(Binder.java:249)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.os.IMessenger$Stub$Proxy.send(IMessenger.java:89)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.os.Messenger.send(Messenger.java:50)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at messenger.demo.MessengerDEMOServiceConnection.onServiceConnected(MessengerDEMOServiceConnection.java:31)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.app.ActivityThread$PackageInfo$Servicedispatcher.doConnected(ActivityThread.java:1247)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.app.ActivityThread$PackageInfo$Servicedispatcher$runconnection.run(ActivityThread.java:1264)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.os.Handler.handleCallback(Handler.java:587)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.os.Handler.dispatchMessage(Handler.java:92)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.os.Looper.loop(Looper.java:123)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at androID.app.ActivityThread.main(ActivityThread.java:4627)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at java.lang.reflect.Method.invokeNative(Native Method)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at java.lang.reflect.Method.invoke(Method.java:521)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:626)12-27 16:42:29.592: ERROR/AndroIDRuntime(832): at dalvik.system.NativeStart.main(Native Method)
没有这些代码,代码就可以了:
Message msg = Message.obtain(null,5); msg.replyTo = mMessenger; try { mService.send(msg); // line 31 } catch (remoteexception e1) { // Todo auto-generated catch block e1.printstacktrace(); }
它可以在没有AIDL的情况下工作,并且可以“单独”使用AIDL.我可以多次拨打onBinder吗?
解决方法Can I call the onBinder more than one time?
简单回答:是的.实际上,如果两个不同的应用程序连接到同一服务,则会发生这种情
那么为什么这不起作用呢?因为您使用两个不同的接口进行通信.
详细情况:
>您连接到服务(AIDL)
>您尝试通过信使发送消息
> AndroID通过AIDL将此消息发送给服务
>首先要做的是,给定的接口匹配(通过比较接口描述符)(有关详细信息,请参阅Stub.onTransact)
> AndroID注意到这些接口不匹配并抛出SecurityException
没有抛出remoteexception,因为尚未调用send()方法.
轻松修复:捕获SecurityException.但我会考虑这种极端糟糕的风格(一如既往,当您的应用程序逻辑在异常时进行中继).
不太容易解决:
为此,两个接口都需要具有相同的接口描述符(在生成的AIDL类中的Stub.DESCRIPTOR中定义).但是你需要自己实现IPC的代理/存根(不是那么糟糕).
另一个修复:创建两个ServiceConnection类.一个负责AIDL,一个负责信使.
最佳修复(IMO):一次确定一种技术.您的解决方案感觉就像开车(单独)同时驾驶两辆车一样.
总结以上是内存溢出为你收集整理的android – AIDL和Messenger同时出现全部内容,希望文章能够帮你解决android – AIDL和Messenger同时出现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)