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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:36:58+00:00 2026-05-27T05:36:58+00:00

In my application, I have a Service which is responsible for looking after a

  • 0

In my application, I have a Service which is responsible for looking after a Bluetooth connection to an external device. This Service class periodically polls the external Bluetooth device for data and adds that latest data into a log held in cache (or possibly SD card) memory.

Amongst the various Activity classes that I have, there is one particular Activity that represents the main UI. It is responsible for displaying, in graphical form, the logged data based upon the cache file data. Let’s call this Activity the Dashboard. The user can scroll back and forth on that graph to review the data collected and logged within the cache since the application was started.

For the purpose of this question, there are two modes of operation to consider. It is possible for the user to select a “log to SD card” option, whereby the application must continue the polling and logging to SD card even when all Activity classes are killed’ (e.g. the user has gone back to the launcher). In this case my Service is started using .startService() and continues to run, and will only be stopped when the user invokes the application again and disabled SD card logging. The other mode is where the user hasn’t selected “log to SD card”, in which case the Service is still managing the Bluetooth connection, polling and logging to cache memory for the purpose of visually displaying the data on the graph, but only needs to do so while the Dashboard Activity is being used.

What I have at the moment is that the Dashboard Activity initially binds to the Service using bindService(), and does a corresponding call to unbindService() within the onPause() method (as otherwise I would of course leak the Service).

The problem is that the Service needs to maintain the Bluetooth connection and continue logging during orientation changes or when the user invokes another Activity over the top (e.g. checks an email). Now if the user has selected “log to SD card” resulting in a call to startService() then of course there is no problem. The problem of course is how to distinguish between an Activity being destroyed and then created again due to orientation (or some other configuration) change, and being destroyed because the user went back to the launcher. In the former case, I don’t want the Service datalogging to have been interrupted. In the latter case, I want the Service to stop, if the user hasn’t selected “log to SD card”.

The best solution I can think of for this at the moment is for the service to be always started using startService(), so that it continues to run when the Dashboard has been destroyed. What I would then do is implement a time-out within the Service, whereby the Service will stop itself unless continuous SD card logging is enabled, or the Dashboard is onCreated again within five seconds (say) and re-binds to the Service. This seems a little crude, and I can’t help thinking this must be a common design problem that has a better solution that I’ve overlooked.

  • 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-27T05:36:58+00:00Added an answer on May 27, 2026 at 5:36 am

    One approach you could use, is check to see if the Activity isFinishing() before unbinding in your onPause(). If it is finishing, you would defer the unbinding to your onDestroy() method. Before onDestroy() is called, however, you could persist your ServiceConnection in the onRetainNonConfigurationInstance() method. If you do perform that persistence, than you wouldn’t actually call unBind() inside of on your onDestroy() at all and simply let the new instance of your Activity do the unbinding.

    That being said, depending on your application, you might find it is just easier to start/stop your service.

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

Sidebar

Related Questions

I have a service which is binded to application context like this: getApplicationContext().bindService( new
Please advise me on this. I have both Wcf service (which will be responsible
I have a C# service which is not an ASP.NET application, but uses a
I have a Windows Service application which uses a Threading.Timer and a TimerCallback to
I have a windows service application which works using remoting. It is used to
I have a J2EE application with a web service which goes like http://servername/service?task=getFile&id=25 How
I have a medium sized application that runs as a .net web-service which I
I have two windows application, one is a windows service which create EventWaitHandle and
I have a selfhosted WCF service application which I want to deploy by a
I have an application which has to live as a service, I create an

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.