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 5849605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:01:26+00:00 2026-05-22T13:01:26+00:00

I am trying to send a message to my main activity from an Async

  • 0

I am trying to send a message to my main activity from an Async task embedded within a Service. Basically, the Async task has to block on input and it can’t run in the main Activity thread (the blocking was removed from the example code below). When the data comes in though, I need to send it to the main activity. I am finding that the messages sent below never make it. If the answer is moving the bind within the Async task, how do you do that? Pointing to example code would be a big help if possible.

public class InputService2 extends Service {
int bufferSize = 1024;
Process process;
DataInputStream os;
TextView inputView;
byte[] buffer = new byte[bufferSize];
private MyAsyncTask inputTask = null;
       public void onCreate(){
           inputTask = new  MyAsyncTask();
           inputTask.execute((Void[])null);

       }
       private class MyAsyncTask extends AsyncTask<Void,Void,Void> {

        int mValue = 0;
        static final int MSG_SET_VALUE = 3;
            protected void onProgressUpdate(Void progress){

            }

            protected void onPostExecute(Void result) {

            }

            protected Void doInBackground(Void... params) {

                int i = 0;


                try {
                    mValue = 0x23;
            Message message =     Message.obtain(null,MSG_SET_VALUE,mValue,0);
                    mMessenger.send(message);
                }
                catch (Exception e) {

                }


            }
        } 
       class IncomingHandler extends Handler {
            @Override
            public void handleMessage(Message msg) {
            }
        }
        final Messenger mMessenger = new Messenger(new IncomingHandler());
        public IBinder onBind(Intent intent) {
            return mMessenger.getBinder();
        } 

}

Below is inside the activity:

class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, msg.arg1, duration);
        toast.show();
    }
}

boolean mBound;
private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the object we can use to
        // interact with the service. We are communicating with the
        // service using a Messenger, so here we get a client-side
        // representation of that from the raw IBinder object.
        mService = new Messenger(service);
        mBound = true;
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        mService = null;
        mBound = false;
    }
};

protected void onStart() {
    super.onStart();
    // Bind to the service
    bindService(new Intent(this, InputService2.class), mConnection,
            Context.BIND_AUTO_CREATE);
}
  • 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-22T13:01:27+00:00Added an answer on May 22, 2026 at 1:01 pm

    It looks like you based your example on the javadoc reference at http://developer.android.com/reference/android/app/Service.html#RemoteMessengerServiceSample, however you left out much of the implementation detail that actually makes it work. You have to go back and implement the full functionality referenced in that example to use that particular pattern: pay careful attention to the REGISTER_CLIENT and UN_REGISTER_CLIENT implementation sections in the IncomingHandler class as these are the bits that actually ensure that the Message can be transferred from the Service to the Activity.

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

Sidebar

Related Questions

I'm trying to send an email message using the .NET MailMessage class which can
I have been trying to encrypt soap message and send to the server, so
I am trying to send an email from a site I am building, but
i'm trying to send fake keyboard input to an application that's running in a
I am trying to send an anonymous object over a web service. Is there
I am trying to send a message with attachment using CDO object. When the
im trying to send a Windows message to my Delphi application, but im having
I'm having problems trying to send an XMPP message to a 'Room' in our
I'm trying to make a program (client) which kan send a message to a
I am trying to send sms from java application using SMPPSim as SMS gateway

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.