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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:10:58+00:00 2026-05-28T17:10:58+00:00

i have a problem with binding a service to an activity in Android. The

  • 0

i have a problem with binding a service to an activity in Android. The problem occurs in the activity:

public class ServiceTestActivity extends Activity {

private static final String TAG = "ServiceTestAct";
boolean isBound = false;
TestService mService;    

public void onStopButtonClick(View v) {
    if (isBound) {
        mService.stopPlaying();
    }
}
public void onPlayButtonClick(View v) throws IllegalArgumentException, IllegalStateException, IOException, InterruptedException {
    if (isBound) {
        Log.d(TAG, "onButtonClick");
        mService.playPause();
    } else {
        Log.d(TAG, "unbound else");
        Intent intent = new Intent(this, TestService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }
}


private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        isBound = false;

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        isBound = true;         
    }
    };
}

isBound tells if the service (called TestService) is already bound to the activity.
mService is the reference to the service.

Now if i call “onPlayButton(..)” the first time, with the service not beeing bound, bindService(..) is called and isBound switches from false to true. Then if i call “onPlayButton(..)” again, it calls “playPause()” on the service object. To here everything works fine.

But i want “playPause()” to be called right after the service has been bound, so i changed my code to this:

public void onPlayButtonClick(View v) throws IllegalArgumentException, IllegalStateException, IOException, InterruptedException {
    if (isBound) {
        Log.d(TAG, "onButtonClick");
        mService.playPause();
    } else {
        Log.d(TAG, "unbound else");
        Intent intent = new Intent(this, TestService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        mService.playPause();
    }
}

From this point on I get a NullPointerException, because mService doesn’t have a reference to the bound service, it’s still null. I checked that by logging the value of mService at different positions in the code.

Any tips on what I am doing wrong here? I am pretty new to programming (especially binding) services in android, but I still don’t see where the major differences between my to versions are.

  • 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-28T17:10:59+00:00Added an answer on May 28, 2026 at 5:10 pm

    One solution is to call playPause() in onServiceConnected(). Another solution is to call startService() with a custom intent that will tell the service to play. I think you might want to think about a redesign. I would try to design the service so that you can start and bind to the service when the activity starts and stop the service when the activity stops. If you need a service that will stay active past the lifetime of the activity, extend the Application class and you can start the service in the onCreate() method.

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

Sidebar

Related Questions

my problem is here: I have some class public class Component { ... private
I have an Android application that is binding to a persistent service (once started
Today, I have a problem with hosting my custom service host using custom binding
I have a problem with binding data using BindingSource, typed dataset and DataGridView. My
I have a problem with a WCF service, which tries to serialize too much
I have come across a problem with binding to a PasswordBox . It seems
The problem... I have a WCF Service, I won't waste anyone's time with copy/pasting
I have a WCF rest service using webHttpBinding that returns JSON result. The problem
I have a service running in my Android application which contains a HashMap that
I'm all excited about binding an activity to a Service and transferring data without

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.