Hello Android Gurus
For API Level 7 and Above-->
I am badly struck trying to figure out a solution for the following problem:
I have an activity which i would use to Kick start an Service. This is an infinite loop service which can run forever. I would like to disconnect the Service from Activity and at a later point of time i will call the activity again which should tell me the current state of Service and i can Stop the service.
Is something like this possible. Right now my Service is Sticky and i am not able to Close the activity as Service is running for ever and i am also not able to Stop the service from activity.
Code samples would be of great help!!! Thanks in advance...
Multiple questions:
You can disconnect/reconnect from the Service using bind. Unfortunately the best advice to give there is to carefully read the documentation for Service at http://developer.android.com/reference/android/app/Service.html: look carefully at the Local Service example, as it demonstrates what you need to do to bind/unbind to a sticky service.
To stop it, once you've bound you can call stopSelf.
Start the service non sticky from the Activity.
In the service control the flow logically, say calling onStart() to keep it running.
Call onDestroy() when you have to stop.
Related
I am trying to do the program to monitor Missed calls. I start a service in Activity, and use BroadcastReceiver in service to monitor Missed calls. How to continue monitor Missed calls after Activity is destroyed?
Now I want to a progress of background to realize this. Thanks in advice.
This is a very good tutorial about service in android:
http://www.vogella.com/articles/AndroidServices/article.html
According to this , your Service will keep working in background until it is stopped explicitly, even if your app is in background or is closed.
Service always keeps running in the background unless you explicitly stop it... Here is a good tutorial that should get you started:
http://mindtherobot.com/blog/37/android-architecture-tutorial-developing-an-app-with-a-background-service-using-ipc/
I have a service in my application but this service stop working after time and i want to keep the service keep working without stopping and for that i try to start the same service from onDestroy method when it dieing but its not work even the written log not appear ,so it is possible to call the service from itself and if it not may i create another service who do the same work and they call each other when one of them stopped, so when in onDestory i cant call it so how to call it?, wish that someone can help me with this situation thnx on advance
You should not try to start the service again yourself. Better return START_STICKY or START_REDELIVER_INTENT from onStartCommand() and the system will do it for you when it has enough resources.
See the following link where there is also an example.
http://developer.android.com/guide/components/services.html
I have a thread (Updater) inside a service (RefreshTasks) that check if there are some updates on the server. Then I have 4 activities that use those data.
But I have a problem in managing this service...
I would like to keep the service active along the whole application, even if the screen goes off. What is the correct practice to manage a service like this? In this moment I start and stop the service every onResume and onPause method of all activities... but this implies that the service will stop when the screen goes off.
Any suggestion?
Thanks AL.
The Service instance continues to run also when your app is paused.
I suggest you to look also this service provided by Google, which is a good solution for push notification in communication between the device and your service:
https://developers.google.com/android/c2dm/
And this is a good tutorial to start with:
http://blog.mediarain.com/2011/03/simple-google-android-c2dm-tutorial-push-notifications-for-android/
I have an Activity that starts a service. When the activity is closed, I want the service to continue running in the background. I have a couple of questions here.
Will closing the activity screen cause the activity to actually stop? Or do I need to forcibly stop it to cause it to stop?
If closing the screen does cause it to stop, then I assume I need to use startService to start it. Is that correct? If that is the case, is there a way to get a handle to the running service next time the activity starts? If it is not the case, then I can just bind to the service.
When your Activity is no longer visible on the screen, it is stopped. Stopping an Activity bound to a Service does not stop the Service. However, you'll want to make sure you unbind from the Service when your Activity calls onDestroy() to make sure you don't have any dangling handlers and suchlike.
You can rebind to a running Service the same way as you did the first time. Sending an Intent to start a Service that's already running doesn't create a second instance of it, so that's safe.
A good way to start a Service like you're describing is to start it using the Context.BIND_AUTO_CREATE argument to your call to bindService().
The details of all of this can be found at the Android docs about bound services. It can be a little confusing at first. Follow the tutorial code closely since it sounds like your problem maps well onto the sample they provide.
I need an advice for my latest app. It will show the user the latest subtitles released, and it will give him a notification in case new subtitles of his favourite series have been released; what should I use to achieve this?
I was thinking to create and run a service which will include a
timer.scheduleAtFixedRate(new TimerTask() {...
but at the same time I really don't know how to make it interact with my app (if the app is opened I don't need any notification but I need to update the GUI).
I could use a thread but I'd like it to run it even after the main activity has been killed...
or I could use a AsyncTask so it would be easier to deal with the Application GUI.
Which solution should I use? I was thinking I should simply use a service (the first solution), but I'm not too sure about it, and furthermore I don't know if there is any way to make a service communicate with an activity periodically...
Thanks for your help.
A service communicating with an activity is called bound service, that's what you should use IMO.
However, you say that when the activity dies, the service should keep running. But if the service is bound to your activity and the activity is dies, the service dies too.
I suggest you to read here about it.
Check and see if you can bind a service to an activity, and when it dies, unbind and let the service continue to run independently.
If you can't, the activity could unbind itself, then start the service independently (with startService rather than bindService).
if you are showing notifications, why not use C2DM messages for communicating with the app. The only thing would be that there would be popups shown to the user even if your app is not running. No need to use threads/services.