Solved

Get running Intent Service

Posted on 2014-03-27
3
760 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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 89
groovy example issue 10 71
recursion example 16 71
Java JRE greater than 1.6 5 24
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now