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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:42:05+00:00 2026-05-27T18:42:05+00:00

I have build a service to broadcast a timer data to various activities. Usually

  • 0

I have build a service to broadcast a timer data to various activities. Usually it runs ok without problems, but there are ocassions when android restarting it does not work as expected.

It has also three Broadcast receivers:

1 – Screen off event: in order to stop broadcasting timer data and setting an alarm in alarmanager for when the service ends, so I will be able to play end notifications for the users to attend.

2 – Screen on event: in order to continue broadcasting the timer data. I also cancel any preiously alarm pending.

3 – A receiver for alarms. This usually fires when screen is off, as explained in 1.

My service is started with startForeground and return START_REDELIVER_INTENT. AS the start intent has inicial timer date, I can recreate service status without problem. Broadcast alarm receiver and onStartCommand share the same handle intent routine to start or continue the service.

All this works perfect. For short timer ( < 30 min ) I dont find any problem. The screen can be on, off, change same times from on to off and from off to on. Also the activity can be in front or in back. I have toying with those all possible states. In all cases, my service and activity runs ok.

My problems comes when some timer are longer ( > 30 min , usually I set up for 35 min). There are sometimes that maybe due to memory reason Android kill me service. That is ok, as I understand Android does this in order to improve user experience. The problem is that when I goes to ‘settings/application/services’ I can see my service in ‘restarting’ state. I suspect this mean Android has not launched it yet and it is been sheduled. It show that state for long time (I have not had patient to say if it changes for more that other half hour….)

The problem is that when in that state, that can long for ever (I have been looking into it and the service is never started), the timer (in my watch) reach the timeout, the alarmmanager launch my intent, but as my service is not started yet and the broadcast not registered, I cann’t do staff for time outs (playing notifications). So the user does not known the service timer has ended and it is a heavy problem.

It is very courious. As the service is killed in almost any run passed 30 min, while other services are not killed.

My question is: what exactly is it happening? How can I correctly handle this situation in order to detect alarms been fired correctly, or more to the point: how can I force my service to be restarted correctly?

For adding some help data:

  • My service does have very little memory compare to others, and also does not run long operations, it only uses handler.postdelayed(“sendUpdatesToUI”,250) when screen is on, and nothing when screen is off, only waiting for alarmmanager to send time out intent.

  • When time is reached, and user opens the activity, it received the timer data broadcast intent from service and as it see time is reached, then it stops the service.

  • I understand and accept user can kill the service when he wants, and I accept that. The problem here is not the user, but Android restarting the service.

  • When service killed, onDestroy is not invoked.

  • Using 2.3.4 version.

  • 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-27T18:42:06+00:00Added an answer on May 27, 2026 at 6:42 pm

    After lot of investigation I found the solution to my problem. It is all about startForeGround function. I was using:

    startForeground(0,not);

    while this looks good and it found in lot of examples around the web, the correct way to do it is not using 0, but any other random value:

    startForeground(2765, not),

    This solve the problem.

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

Sidebar

Related Questions

I have a windows service which fetches data from various datasources and build a
I have two servers with TFS installed, one that runs the build service, and
I have build a windows service using a timer which is not ideal for
I have build a wcf web service that simply provides MS Dynamics SQL data
I have a spring-based Web Service. I now want to build a sort of
I have an application that sends messages to an external web service. I build
I want to build a REST web service on app engine. Currently i have
I have some collegues who have build a WCF Service. Their security settings are
I have to build a service for different kind of populations. This service contains
I have build a JSON string (to be posted to a web service), and

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.