I want to start service when I exit from my app.
code is :
Intent i_service = new Intent(MainActivity.this,check.class);
startService(i_service);
And when I re-open my app , I want to stop my service which I started when I exit from my app.
code is:
Intent i_service = new Intent(MainActivity.this,check.class);
stopService(i_service);
This is the right way to do it ?
How can stop my old service?
thanks in advance
Check Your service is run or not and then stop ur service on oncreate() of ur activity as below,
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (check.getName().equals(service.service.getClassName())) {
Intent i_service = new Intent(MainActivity.this,check.class);
stopService(i_service);
}
}
I think you are doing it in the correct way. Just that when the Application is started again, you need to first check whether it is running or not, and then stop it.
The answer with code described by "MSS" above explains it well!
Related
I have an app which has a single activity which has a checkbox the starts and stops a foreground service. The activity also has some other settings which control that service.
The problem is that after some time navigating other apps the service continues to run fine but the activity gets killed. If I restart the activity the checkbox will be set to default, off. If I switch it to on, a second service starts, while the first one is still running and can't be controlled anymore.
I can't figure out how to handle this situation. It's necessary for the service to run indefinitely and there doesn't seem to be anyway to keep the activity from dying. Maybe there's a way for the new activity to find the old service?
Try to kill the service in OnPause() method of your activity .that solve my problem few times ago.
To check if particular service is running, write the below method your activity, and call it when you want to check the state of the service
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
"service continues to run fine but the activity gets killed"
services are given more importance than activities, if android where given a choice to kill an activity or a service it always kill an activity first.
"a second service starts"
services can only be started once no matter how many times you start a service.
"checkbox will be set to default"
use sharedpreferences to persist values.
I have a Foreground Service for my app, which must run the whole time and never stops. While I was testing it few times, it seems after 3-4 days Android decides it just to stop it, and never restart it again, and in my Activity I have a check for the service if its running, and if it isnt to start it.
Even though, the service is not starting, and I can't debug it, because this is not happening immidiatelly.
So this is my check, is there something wrong in it?
if (!isMyServiceRunning(MyService.class)) {
startService(new Intent(this, MyService.class));
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
I call this check in onResume() of the Activity, and I can't just run it everytime when the activity starts, because I have a constantly running Thread in the service, which will get more than one instance if started several times...
So here is what I want:
Either have more reliable way to check for the service if its running
or
Just start the service every time the activity starts, to prevent problems, and the thread inside of it must always have just 1 instance (I have already declared is as static)
You can make your service Bindable and bind your Activity to the service when the Activity starts/resumes. This way you will be sure the service is running. But I think you have a problem with your service. My I ask you to post more information about the service? It is not normal the OS to kill a foreground service with no traces.
I searched a lot in the net about it, the solutions are based on onPause and onDestroy. I want to give a library to a developer who just needs to paste a few lines of code from the library in his app which will enable the developer to create a service and destroy it when his entire app is in the background.
Does the Android OS send some kind of signals or intents when the app or the activity is changed (other than onPause or onStop method), so that i can catch that in a broadcast receiver from my library and do some actions.
If you know the package name of your app, try this (put this following snippet in the onCreate method of your app):
ActivityManager am= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
Then,
boolean exit = false;
while(!exit)
{
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(!componentInfo.getPackageName().equals("Your package name"))
{
//Do your work here
exit = true;
}
}
When you start your app, this will be put into componentInfo. The taskInfo.get(0).topActivity will return the activity in the foreground. Hence you can know that your app has gone to the background by comparing package using the second code snippet.
Note:Put this second code snippet in an Asynctask so that the checking of whether the app has started can be done in the background.
I want to create a single button which serves both functions of Starting and Stopping a service.
Also I want to make sure that even if user quits the application and again comes back then according to whether service is running or not, I want to show appropriate Text on Button.
So in short, With what thing I can get to know if service is running or not ?
You can check if service is running or not with below code. Rest of the question is logic based once you find the service is running or not.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
I refer this answer just check it
what is the best way to implements a service from starting with alarm manager when android applications first starts?
Right now i have a Home activity which is the first page of the activity which runs the service when it is being created. However when the users were to start the application again on the app menu (when the application is running.), the home activity will be run again and the service will start again.
Is there any better alternative for this way of application?
Follow this link... have a look at the implemenation below.
http://blog.gregfiumara.com/archives/82 ...
and for the checking whether a service is active or not... you can simply use this.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.background.BootableService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Use this in the any class you want to check for a existing service... and if no service is present then just create one or else you are good.
Let me know how it works out.