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
Related
In my app I have created a service, suppose MyService, and what i want to do is that I only want to run it if i am not inside the app. So when I open my app, I want to check if any instance of MyService is running/exists. If found I want to stop it. And when I quit my app will start the service again using intent.
Most of the search results I have found suggests to use stopService() method. But i can use that only if I have an intent pointing to that service, right? But the only solution I've found regarding the 'search' process so far suggests using getSystemService(Context.ACTIVITY_SERVICE). But i get only an RunningServiceInfo object through it. How can i use this object to stop the running service?
And if it's not possible, what is the most efficient way to accomplish what I'm trying to do here?
You can call stopService() regardless if the service is running or not, so you don't need to check whether it's running or not. If it is, it will be stopped and stopService() returns true; otherwise stopService() returns false (but it sounds like you don't care about the return value anyway).
See the documentation for stopService().
getSystemService() is not useful for finding your own service, it's for finding system-level services like the ActivityManager or the AlarmManager.
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 started a service in Activity.Now, if i force close the activity then automatically the service is also force stopped.Is there any way to restart it automatically.?
Thanks in Advance.
You must implement the onStartCommand() method by returning the START_STICKY or START_REDILIVER_INTENT, but it's your responsibility to stop the service by calling stopService() or stopSelf() method.
You should return START_STICKY from onStartCommand in your service. This will make sure Android restarts your service after being killed. See the link bellow.
http://developer.android.com/reference/android/app/Service.html#START_STICKY
This will not deliver again the original intent. If you also need that return START_REDELIVER_INTENT.
These are also explained in the Android blog.
http://android-developers.blogspot.com.au/2010/02/service-api-changes-starting-with.html
Does android send any kind of broadcast when a new service is started/restarted.
underlying reason is i'd like to catch whenever a background service is starting without my knowledge.
any answer to how it's possible, if it is, are appreciated.
EDIT:
I want it to work with all services, not only my own services.
If the background service is in your control then you can use onBind() method to get to know that the service is started by bindService() and same time you can use onStartCommand() method if the service is started by startService()....
That mean you want to access system service or what? or service which you have written in your android application?
if you want to control your service which you have written in your application then as above told you can control it..or if you want to start or stop service when you want then you can create broadcast receiver and write onreceive() what you want to do.
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.