Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:40:18+00:00 2026-05-27T18:40:18+00:00

I try to write a demo about how to use the AIDL and the

  • 0

I try to write a demo about how to use the AIDL and the Messenger at the same time, but I have an error, and I don’t know the reason.

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

And the stack trace:

12-27 16:42:29.412: INFO/System.out(832): MessengerDEMOServiceConnection SocketServiceConnection()
12-27 16:42:29.442: INFO/System.out(832): MessengerDEMOService onCreate
12-27 16:42:29.452: INFO/System.out(832): MessengerDEMOService onBind()
12-27 16:42:29.452: INFO/System.out(832): MessengerDEMOService onBind() AIDL
12-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 VM
12-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: main
12-27 16:42:29.592: ERROR/AndroidRuntime(832): java.lang.SecurityException: Binder invocation to an incorrect interface
12-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)

Without these lines the code is working:

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

It works without the AIDL and works the AIDL “alone”. Can I call the onBinder more than one time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:40:19+00:00Added an answer on May 27, 2026 at 6:40 pm

    Can I call the onBinder more than one time?

    Simple answer: yes. Actually this does happen if two different applications connect to the same service.

    So why doesn’t this work? Because you’re using two different interfaces for communication.

    What happens in detail:

    1. You connect to the service (AIDL)
    2. You try to send a message by the messenger
    3. Android sends this message to the service by AIDL
    4. First thing to do is, that the given interfaces do match (by comparing an Interface Descriptor) (See you Stub.onTransact for details)
    5. Android notices that these interfaces do not match and throws a SecurityException

    There is no RemoteException thrown because the send() method isn’t invoked yet.

    Easy fix: catch the SecurityException. But I’d consider this extremly bad style (as always it is when your application logic relays on exceptions).

    Not so easy fix:
    For this to work, both interfaces would need to have to same interface descriptor (defined in Stub.DESCRIPTOR in your generated AIDL class). However you’d need to implement the Proxy/Stub for IPC by yourself (not that bad).

    Another fix: Create two ServiceConnection classes. One responsible for AIDL, one for the messenger.

    Best fix (IMO): Decide for one technique at a time. Your solution feels like driving to work (alone) with two cars at the same time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to write a demo with WCF (REST) Service, and I use LinqToSQL
I try to write :ab in Vim for faster coding but the question is
I try to write a site with CodeIgniter but I've a problem with PHP.
i try to write my own control.But my control' html has problem: Look like
So, for some reason when I try to use a for loop to initialize
I have a tech demo application to write, with multiple clients: web client (ASP
try to write a composite component that allows mutltiple text inputs. I read that
I try to write Activator action for changing current song rating. I now that
I try to write a Facebook app. This app will communicate with a rfid
I try to write a javascript Self-Executing Anonymous Function window.App = window.App || {}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.