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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:06:41+00:00 2026-05-25T14:06:41+00:00

I’m writing a program that should run every 10 minutes in the background. The

  • 0

I’m writing a program that should run every 10 minutes in the background. The code I have seems to work fine as long as I’m actively using my phone, but after a long period of time, say overnight, the process seems to stop on it’s own. When my program is running as it should I can view it under the “cached process” on my device, but then it will stop showing in the list after awhile.

I was reading about WakefulIntentService and was wondering if I need to use that. As I understand it, it will keep your background process running even if the phone sleeps. Not sure what “sleep” means in Android, if it’s when you power off, or does the phone go into a “sleep” state if it’s not used for awhile.

This is the code I’m using:

Main class:

 public class Main extends ListActivity
 {
      @Override
      public void onCreate(Bundle icicle) 
      {
           AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

           Intent myIntent = new Intent(this, AlarmReceiver.class);
           PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
           alarmManager.cancel(pendingIntent);

           alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 600000, 600000, pendingIntent);
      }
 }

BroadcastReceiver class:

 public class AlarmReceiver extends BroadcastReceiver 
 {
      @Override
      public void onReceive(Context context, Intent intent) 
      { 
           context.startService(new Intent(context, MainService.class));
      }
 }

Service class:

 public class MainService extends Service  
 {  

protected void handleIntent(Intent intent) 
{
    // obtain the wake lock 
    PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); 
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); 
    mWakeLock.acquire(); // check the global background data setting 
    ConnectivityManager cm = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE); 

    if (!cm.getBackgroundDataSetting()) 
    { 
        stopSelf();
        return; 
    }

    new FetchItems().execute();
}
 }

 private class FetchItems extends AsyncTask<Void, Void, Void> 
 {
    protected Void doInBackground(Void... unused) 
    {           
      SomeLongProcess();
      return null;
    }

    @Override 
    protected void onPostExecute(Void result) 
    {   
        stopSelf();
    }
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId)
 {
   handleIntent(intent);
   return START_STICKY;
 }

 @Override 
 public void onStart(Intent intent, int startId) { 
   handleIntent(intent); 
 }

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

         AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

         Intent myIntent = new Intent(this, AlarmReceiver.class);
         PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
         alarmManager.cancel(pendingIntent);

      mWakeLock.release();
}

AndroidManifest.xml:

<receiver android:name=".AlarmReceiver"> 
    <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter> 
</receiver>
  • 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-25T14:06:42+00:00Added an answer on May 25, 2026 at 2:06 pm

    Does it really have to run every ten minutes even when the device is asleep? If it doesn’t, use AlarmManager.ELAPSED_REALTIME and your service will be run when the device is wakes up, saving a lot of battery. As for your question, you can assume that the screen going dark == going to sleep (of course, if other services are holding wakelocks, that is not the case).

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a text area in my form which accepts all possible characters from

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.