Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get running Intent Service

Posted on 2014-03-27
3
Medium Priority
?
961 Views
Last Modified: 2014-04-02
Hi,

I'm creating an android application that will do a task in a specific time (defined by the user). For this I'm using an Intent Service.

The the problem is if the user closes the application (example: using back button) then I cannot get the the running service to shutdown it, to read information from it (to update my activity when the application opens), etc.

Is this the better approach?

Is there any way to get the running service (intent service)?

Thanks
jpaulino
0
Comment
Question by:jpaulino
  • 2
3 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 39961474
When the user closes your activity the onStop() method of the activity is called (or the onPause(), depending on what made the activity stop). So you could implement your code in one of these two methods.
0
 
LVL 48

Author Comment

by:jpaulino
ID: 39961535
Thanks for the reply.

But the second time the user presses the back button it destroys the activity and the next time it opens, it starts  a new instance.

What I want to achieve is to check if the service is already running. If not, then start a new one, if it's running, then get the state of the timer, selected options, etc, and event shutdown the service (if the user wants it).
0
 
LVL 35

Accepted Solution

by:
girionis earned 1500 total points
ID: 39961595
You are right, when the user presses the back button your current activity will be destroyed. So when you start your activity again it will be a new instance. You cannot check if your activity is running since there is no activity. I guess what you need to do is to recreate the activity state when the onCreate() method is called (when you start a new activity). You will need to save your activity state in method onSaveInstanceState() and then recreate it by reading the data from the bundle. For more information have a look here.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question