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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:40:55+00:00 2026-06-10T02:40:55+00:00

in onDestroy() I check whether the service is still running using the code below.

  • 0

in onDestroy() I check whether the service is still running using the code below. If it is – I unbind and stop it.

public boolean isServiceRunning(Class<?> serviceClass) {
        String serviceClassName = serviceClass.getName();
        final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

        for(RunningServiceInfo runningServiceInfo : services){
            if(runningServiceInfo.service.getClassName().equals(serviceClassName)){
                return true;
            }
        }
        return false;
    }

Now I have a situation when isServiceRunning returns false, but after onDestroy() I get an error saying that ServiceConnection has leaked. Why would that be?

Edit:

That’s how I start the Service (in onCreate()):

startService(posServiceIntent);
bindService(posServiceIntent, posConn, BIND_AUTO_CREATE);

and

posServiceIntent = new Intent(getApplicationContext(), PositionService.class);

private ServiceConnection posConn = new PosServiceConnection();
public class PosServiceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "PosServiceBinder connected [name: " + name.toShortString() + "].");
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "PosServiceBinder disconnected [name: " + name.toShortString() + "].");
        }
    }

protected void onDestroy() {
        if(isServiceRunning(PositionService.class)){
            Log.d(TAG, "Stopping PositionService in " + MainActivity.class.getSimpleName() + ".onDestroy()");
            unbindService(posConn);
            stopService(posServiceIntent);
        }
  • 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-10T02:40:57+00:00Added an answer on June 10, 2026 at 2:40 am

    You need to call unbindService() in onDestroy(). Stopping the service won’t make it stop if it has bound connections.

    In any case, the error “ServiceConnection leaked” appears because you still have a bound connection to the service.

    EDIT: Add additional observation

    You wrote:

    “I check whether the service is still running using the code below. If
    it is – I unbind and stop it”

    That won’t prevent leaking the ServiceConnection. You need to call unbindService() when your activity shuts down even if your service is no longer running. Make sure to put the call to unbindService() in a try/catch block because it is posible to get an IllegalArgumentException which you can safely ignore (it means you have no connection to the service).

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

Sidebar

Related Questions

I have this code running in my onDestroy function: @Override protected void onDestroy() {
I am using the following snippet to check whether applications that I finish() ed
I have a long-running cleanup operation that I need to perform in onDestroy() of
This is the SMS observer code. I need to check only sent sms. When
first I used onDestroy() method as below to make something when my app is
I have service with thread, From activity I am calling start service(from Activity::onDestroy()) and
I am using the following code to removes childs on every viewgroup: protected void
I have a service running in its own process. It appears fine in the
I would like a clean way to check whether a preference changed. This check
I'm using the wakeful service pattern of the commonsware books. My problem is to

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.