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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:03:34+00:00 2026-05-27T13:03:34+00:00

The client code for binding to a service, which is normally in the activity

  • 0

The client code for binding to a service, which is normally in the activity class; I’m trying to move it to the service class, so that the activity class would be as clean and small as possible.

i.e. basically trying to merge the code in the second box here into the first box = as much of it into the service class as possible

Single Line in Activity for Binding to Service

public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Bind to service with this line only:
    AService.bindService(this);
}
}

Static bindService and ServiceConnection Moved to Service

public class AService extends Service {

public String test = "I want to see this";
public static AService aService;
private static boolean isBound;
private static Context context;

// ... IBinder, onBind etc also here on service side

public static void bindService(Context context) {
    try {
        Log.i(TAG, "bindService Start");
        if (!isBound && context != null) {
            Log.i(TAG, "Binding");
            context.bindService(new Intent(context, AService.class),
                    serviceConnection, Context.BIND_AUTO_CREATE);
            isBound = true;
            Log.i(TAG, "Bound");
        }
    } catch (Exception e) {
        Log.e(TAG, "bindService", e);
    }
}

private static ServiceConnection serviceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        try {
            Log.i(TAG, "onServiceConnected Start");
            aService = ((AService.LocalBinder) service).getService();
            if (aService != null)
                Log.i(TAG, aService.test);
            Log.i(TAG, "onServiceConnected Finish");
        } catch (Exception e) {
            Log.e(TAG, "onServiceConnected", e);
        }
    }

    public void onServiceDisconnected(ComponentName className) {
        try {
            Log.i(TAG, "onServiceDisconnected");
            aService = null;
        } catch (Exception e) {
            Log.e(TAG, "onServiceDisconnected", e);
        }
    }
};


public static void unbind() {
    try {
        Log.i(TAG, "unbind start");
        if (isBound && context != null) {
            Log.i(TAG, "Unbinding");
            context.unbindService(serviceConnection);
            isBound = false;
            context = null;
            Log.i(TAG, "Unbound");
        }
    } catch (Exception e) {
        Log.e(TAG, "unbind", e);
    }
}

}

But onServiceConnected is Never Called?

The log shows everything up to:

...
Bound
  • But NOT onServiceConnected Start or beyond
  • and no exceptions.
  • Note that when the same code was in the Activity, it works (when called with MyActivity.this)

What am I doing wrong?

  • 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-27T13:03:34+00:00Added an answer on May 27, 2026 at 1:03 pm

    Is this

    AService.bindService(this);
    

    much better than this?

    bindService(new Intent(context, AService.class),
                    serviceConnection, Context.BIND_AUTO_CREATE);
    

    And does ServiceConnection implementation sit in Activity really annoying your so much? I doubt that.

    I don’t see any point centralize everything into Service and then call a static method in the actual Service to start this Service from Activity. The best practice is to follow the standard way that Google’s recommended to do things, by doing this in your way, you make your code obscure and confuse other people when reading your code (if you work in a team). It doesn’t make any sense IMO.

    Instead of put all your effort into isolate every single bit of service from activity, I would rather consider more on how to isolate business logic from activity and centralize them into service, and let Activity mostly focus on UI stuff.

    Really hope that would help you.

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

Sidebar

Related Questions

I am implementing a WCF web service which interacts with a client whose code
I have some cross platform DNS client code that I use for doing end
After trying to understand why client code is not rendered in a page (injected
I have an ASP.NET MVC server app and client code which uses jquery. I'd
I deployed a JAX-WS Service and use wsimport to generate client code. Because I
I've currently written code to use the ServiceContractGenerator to generate web service client code
I have a simple WCF service that I call server side from code behind
I am trying to create a WCF client that operates with an http rest
I am having a rather frustrating issue trying to call a web service that
I would like to consume a CXF web-service from a .net c# client. We

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.