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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:31:22+00:00 2026-06-11T06:31:22+00:00

I am quite new to Android dev and struggling with with the concept of

  • 0

I am quite new to Android dev and struggling with with the concept of bound services

I have bound a service to an Activity in the onCreate method of the activity

startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

mConnection is a ServiceConnection object for which I have overridden the onServiceConnected callback method in an anonymous class

In the callback, mService (which is a class member of type LibraryService, LibraryService is my subclass of Service) is set to reference the LibraryService object we are binding to:

mService = binder.getService();

I thought that after the call to bindService in onCreate I would then be able to call the LibraryService through mService reference, but it is still set to null at this point indicating that the onServiceConnected callback has not been executed.

I have tried to print the value of a string generated by an mService method using Log:

if (mService != null) {
    Log.d(LOG_TAG, "In onCreate getLogFileName string value: " + mService.getLogFileName("pulse"));             
} else {
    Log.d(LOG_TAG, "In onCreate getLogFileName string value: null");                
}

as I mentioned – it was still null in onCreate, I then added the same log code to onStart and onResume but mService is still null

My question is when does that callback get executed? I would have thought it would have to occur within the body of the onCreate method but clearly not. I think I need a few pointers in how/when callbacks are executed by the Android system

I know the callback does get executed as I have put a breakpoint on the statement where mService is instantiated and it hits that statement. I just don’t understand when its occuring, i.e. how do I know when its OK to assume mService has been set? Can I only access it within the body of the OnServiceConnected callback?

  • 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-11T06:31:23+00:00Added an answer on June 11, 2026 at 6:31 am

    Service binding is asynchronus and, as such, you have to handle this with a little of synchronization.
    Hence, after calling bind service you could do something like

    synchronized(this){
       while(mBoundInstance == null)
          this.wait();
    }
    

    in onServiceConnected:

    synchronized(this){
       this.notify(); //wakes up thread: now mBoundInstance is 100% != null
    }
    

    Note that is good putting that wait in an asyncTask, so that ui does not get frozen.

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

Sidebar

Related Questions

I am quite new to Android dev, but not Java dev, so doing the
I'm quite new to android, but I have already created several apps and published,
I'm quite new to Android and I have been reading the Professional Android 2
I'm quite new in Java android development. So I have an issue with my
I'm quite new to Android and have been using an AVD to debug my
I'm quite new to Android but already have some experience with Java itself. Now
I'm quite new to Android development (started 2 days ago) and have been through
Quite new to Android dev so there is a good chance I made a
I'm quite new to android programming and I have the following problem. I want
So I am quite new to Android, and have limited previous Java experience, so

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.