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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:10:50+00:00 2026-05-27T22:10:50+00:00

I have database cleanup that I need to do within a running service thread.

  • 0

I have database cleanup that I need to do within a running service thread. Under normal conditions, this will occur when the service is done and onDestroy() gets called.

I’ve noticed that if I reinstall or update the application while the service is running that onDestroy() is not called for the service. Is there anyway my application can know at the next startup that it’s the first run after a reinstall so it can complete some cleanup on the database?

To clarify, simply running the service again won’t do what I need. My service in many cases is started by a broadcastreceiver and not by the activity itself so I can’t rely on the onPause for the main activity. I can’t figure out how to know if the service might already be running or was silently killed and needs cleanup.

I’d like to clarify since others seem to have misunderstood. First, onDestroy just plain doesn’t happen in the situation of a reinstall or update of the app. Neither do the shared preferences get reset because the app was not uninstalled. Second, Google specifically states you can’t rely on the onDestroy, so berating me for finding a quick fix instead of asking Google why onDestroy isn’t working doesn’t quite make sense either. See the API docs about onDestroy for Activities. The service documentation doesn’t repeat that warning but obviously it holds true. The only working solution I’ve found to date is the one I posted below from another StackOverflow thread.

  • 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-27T22:10:51+00:00Added an answer on May 27, 2026 at 10:10 pm

    Aha, I found an answer here:
    How to check if a service is running on Android?

    With this little snippet. Just pass a current context to it, so if coming from a BroadcastReceiver pass the context given the onReceive for example… With this I can set a global variable when the service starts, and clear it in the onDestroy. When my app starts if the variable is set I can test to see if the service is currently running and if not then I can do a cleanup.

    private boolean isMyServiceRunning(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.myapp.myservice".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a database that contains a date and we are using the MaskedEditExtender
I have some code that uses an Informix 11.5 database that I want to
Background - I have a client that has an Oracle database with many procedures
I have a database issue that i currently cannot wrap my head around with
I have a script that dumps the db and copies it to S3. This
I have a Visual Studio 2005 BIDS project that exports about 30 database tables
I've inherited a database that wasn't designed exactly optimally, and I need to manipulate
I have a model that works with one user in the database. It is
I have database with many tables. In the first table, I have a field

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.