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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:08:12+00:00 2026-06-12T22:08:12+00:00

This question is kind of a balancing act between the more philosophical question of

  • 0

This question is kind of a balancing act between the more philosophical question of how to write good and nice programs and the rather fact based question of how to preserve battery and system resources. Specifically, its about Android-services that, to perform the task needed, have to run permanently in the background on the phone.

In my case I have several services running in the background. One scans if the phone is connected to WiFi and, if so, sets a variable, one checks for the location and updates the location if a precise enough location has been found, one checks the proximity-sensor, one analyses the noise level, on checks an RSS-Feed for updates…

The app has several problems and I think they may stem from this design decision. Mostly, (except with the location-service and the proximity-sensor which offer updates through the SensorEventListener) the architecture of all my services looks like this

public class ArchetypicalService extends Service
{
Checker checker;

public SharedPreferences prefs;
public final static String PREFS = Constants.PREFS;

@Override
public IBinder onBind(Intent intent) 
{
    return null;
}

@Override
public void onCreate() 
{
    prefs = getSharedPreferences(PREFS, 0);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    checker = new Checker();
    checker.run();
    return START_STICKY;
}

@Override
public void onDestroy() 
{
    super.onDestroy();
}

private class Checker implements Runnable
{
    private Handler handler = new Handler();
    public static final int checkingDelay = 1000; // this number varies depending on what purpose this service has
    @Override
    public void run() 
    {
        //what happends here depends on what the service does
        handler.postDelayed(this, checkingDelay);
    }
}
}

The services are startetd when the app is started. Like this:

Intent intent = new Intent(this,ArchetypicalService.class);
startService(intent);

Thanks to START_STICKY the service will keep running even when we stop the activities. This all seemed text book to me when I wrote it but the more I work with android the more I come across blog-entries that tell me that permanently running services should not be implemented. That they are, above being bad style, make it more likely for the app to crash and eat up the battery and phone-resources. This may all be right: my app crashes a lot and depletes the battery.

So, my question is: If a functionality, like a sensor sweep or checking data on the internet, has to be done regularly in the background, even if all activities have been closed, is there any better way to implement it? (and while I welcome all answers or ideas I would welcome those that provide some helpful example code even more)

  • 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-12T22:08:13+00:00Added an answer on June 12, 2026 at 10:08 pm

    http://developer.android.com/guide/components/services.html. Services do notcreate seperate threads. You need to run them in threads if its a long running operation. So if there are lot of services then running everything on the main ui thread will consume memory. Make sure to run in threads. Might solve your app crash problem. Also check this link. How can I keep requesting a page every 5 seconds and not kill the battery?.

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

Sidebar

Related Questions

Based on this question : Should thoses kind of service go injected in a
This is kind of based off of this question . I'm currently looking at
This question kind of starts where this question ends up. MATLAB has a powerful
This question is kind of a follow up to this question I asked a
This question is kind of related to another question but I have a specific
I know this question has kind-a started religious wars in past and there might
Kind of like this question I have many text snippets that I use many,
As a kind of follow on to this question , what specific services are
I am kind of new to C# and Asp.Net, so this question might sound
I have this kind of question. In my form, i got this as a

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.