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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:33:56+00:00 2026-05-25T22:33:56+00:00

I have a service that autoupdates a database in a given time interval. To

  • 0

I have a service that autoupdates a database in a given time interval. To do this it gets information from the Internet.

I need to have it unbound, so that It runs over all activities. But when the application is closed, it would be nice to terminate the service. To prevent battery drain. How can this be achieved?

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

    I think that you should let your service be started by a boot broadcastReceiver, then ask AlarmManager to relaunch it every now and then.

    public class DbUpdateService extends Service {
      //compat to support older devices
      @Override
      public void onStart(Intent intent, int startId) {
          onStartCommand(intent, 0, startId);
      }
    
    
      @Override
      public int onStartCommand (Intent intent, int flags, int startId){
       //your method to update the database
       UpdateTheDatabaseOnceNow();
    
       //reschedule me to check again tomorrow
        Intent serviceIntent = new Intent(DbUpdateService.this,DbUpdateService.class);
        PendingIntent restartServiceIntent = PendingIntent.getService(DbUpdateService.this, 0, serviceIntent,0);
        AlarmManager alarms = (AlarmManager)getSystemService(ALARM_SERVICE);
        // cancel previous alarm
        alarms.cancel(restartServiceIntent);
        // schedule alarm for today + 1 day
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);
    
        // schedule the alarm
        alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), restartServiceIntent);
        return Service.START_STICKY;
      }
    
    }
    

    To start your service at boot time use this :

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class serviceAutoLauncher  extends BroadcastReceiver{
    
    @Override
      public void onReceive(Context context, Intent intent) {
          Intent serviceIntent = new Intent(context,DbUpdateService.class);
          context.startService(serviceIntent);
      }
    
    }
    

    Finally add this to your manifest to schedule your serviceAutoLauncher to be launched at each boot:

        <receiver android:name="serviceAutoLauncher">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
                <category android:name="android.intent.category.HOME"></category>
            </intent-filter>
        </receiver>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Service that downloads a file from the internet. What I want
I have a service that calls database to retrieve data; this service will return
I have a service that accepts callbacks from a provider. Motivation : I do
I have service that takes some entity and needs to save/update this entity: http://myhost.com/rest/entity
I have WCF service that is hosted in IIS . I need to initialize
I have a service that reads an xml document from a directory(works OK), saves
I have a service that is responsible for reading and writing from a server.
I have WCF service that uses wsHttpBinding and authentication with certificate. I run this
I have a service that will send out notifications with timed tasks. I need
I have service that I need to run every 5 minutes. I already have

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.