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

  • Home
  • SEARCH
  • 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 8068027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:31:26+00:00 2026-06-05T12:31:26+00:00

I am trying to get two-way communication between my Service and Activity set up.

  • 0

I am trying to get two-way communication between my Service and Activity set up. To do this, I start my Service like normal, send a Message with the replyTo set to the Messenger in my Activity, and send it to my Service. That way I have a reference to send messages in both directions. Here is some relevant code in my Activty:

private Messenger mService = null;
private final Messenger mMessenger = new Messenger(new IncomingHandler());

private ServiceConnection mConnection = new ServiceConnection()
{
    public void onServiceConnected(ComponentName className, IBinder service)
    {
        mService = new Messenger(service);
        mBound = true;
    }

    public void onServiceDisconnected(ComponentName className)
    {
        mService = null;
        mBound = false;
    }
};

private void initMessenger()
{
    Message messageHandler = Message.obtain(null, NetworkService.MSG_GET_HANDLER, 0, 0);
    messageHandler.replyTo = mMessenger; //Set the replyTo to give a reference
    try
    {
        mService.send(messageHandler);
    }
    catch(RemoteException e)
    {
        e.printStackTrade();
    }
    catch(NullPointerException e)
    {
        e.printStackTrace(); //Why!!!
    }
}

public void onCreate()
{
...
    //Create the intent to start the network service
    Intent iService = new Intent(this, NetworkService.class);
    int[] connectionConfig = {CONNECTION_BT};
    iService.putExtra(CONNECTION_CONFIG, connectionConfig);
    bindService(iService, mConnection, Context.BIND_ABOVE_CLIENT);
    startService(iService);
    initMessenger();
}

A NullPointerException is thrown when I try to call mService.send(), most likely because mService is null at the time. If I move the initMessenger() code inside a Button’s OnClickListener it works fine which is where the confusion comes in. Am I supposed to wait a certain amount of time after starting a Service to use it?

  • 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-06-05T12:31:27+00:00Added an answer on June 5, 2026 at 12:31 pm

    mService being null at this stage suggests that by this time your Service has not (yet) called onServiceConnected().

    Unfortunately the documentation doesn’t suggest there would be a gap between bindService() returning and the method in the ServiceConnection being called.

    Infact, bindService() should return true when the Service has been started meaning once the code passes this stage the Service should either be started or not. Therefore I would check if bindService() has returned true or not before calling initMessenger() as it’s possible the Service has failed to start correctly.

    It’s also possible that the code within onServiceConnected() is throwing an exception (which, since it is possible it’s being called on a worker thread, could not cause your app to crash), you may want to check back in your log’s to check if this is the case.

    Also, startService(iService); is redundant in this context, bindService() starts the Service, calling startService on the service simply send’s it a message.

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

Sidebar

Related Questions

I'm trying to implement two-way asynchronous communication in C++. I'd like to be able
I am trying to get all logfiles between two dates with regular expressions. My
I'm trying to create two functions: get & set, that both accept an arbitrary
I am trying to access a WCF web service, that is using two way
I'm trying to get the Canvas to use two fonts when drawing text. This
I'm trying to join two DataTables together in a similar way to this question:
I am trying to get the diff between two nearly identical tables in postgresql.
I'm trying to achieve two-way binding between a DataGridView and a BindingList that provides
I am trying to trouble shoot a two way SSL handshake mechanism. I get
Trying to get two checkbox controls to work with each other. Pseudo-code example: If

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.