How to get know when app removed from background? - android

I have some tasks which is running in service. I need it to be running even if the app goes in to background. Its working fine. The problem is, How do I know When my app is removed from background?? In this scenario, I cannot stop my service and which leads to crash. Anybody please help, Thanks in advance.

I think this way you can prevent to stop background service,
In your Service class you need to return START_STICKY in onStartCommand;
Like this,
#Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
return START_STICKY;
}
If someone try to force stop service from background process, then android automatically restart your service with START_STICKY.
I hope this will help you.

I think implementing the onDestroy() method in your main class from where you are binding the Service can help

Related

Android foreground service gets killes after 24 hours

I am new to Android development and have googled a lot. Unfortunately without success.
My problem is the following:
I want to retrieve data from the device every 5 minutes in the background and then process it further. The whole thing should run 24/7.
Therefore I have created a Foreground Service which also shows a notification. The service starts with START_STICKY.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
setSERVICE_NOTIFICATION_ID(R.string.tab_text_locationprofile);
helperNotification = new HelperNotification(this);
startForeground(R.string.tab_text_locationprofile, helperNotification.createForegroundNotification(this, R.string.tab_text_locationprofile));
doWork();
return START_STICKY;
//return START_REDELIVER_INTENT;
}
Additionally I have created the permission in the Android Manifest.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Maybe it is interesting to know how I stop the service.
public void onDestroyDefaults(){
helperNotification.cancelNotification(SERVICE_NOTIFICATION_ID);
//stopForeground(true);
//stopSelf();
}
The whole thing runs fine so far. But after 24 hours the service and the app will be killed.
Many people have this problem, but I haven't found a solution for me anywhere.
Can anyone help me here?
Maybe someone can tell me another solution to do such things.

Android service restarts after Task is removed and i want it to keep it continue

I created a service class that should run in background.And it is working well.
The problem is when i remove task from recent panel it restarts and i loose all data stored in my service class.
I tried almost every way available on internet.
Search for a music player named Phonograph
It does just what i want, song keeps playing without any pause even after removing it from overview screen.
Service Class
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
Manifest
<service
android:name=".service.MS"
android:enabled="true"
android:process="com.architjn.ms"></service>
-> Don't mark it as duplicate as i already tried previously asked similar questions, non of them worked for me.
You should use startForeground in your service
http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
Here is an example that maybe useful
https://github.com/imjarp/101AndroidExamples/blob/cbc64af4748f1f8876138ac077216fd8ab19840d/15-ParallelExecution/app/src/main/java/com/example/jarp/parallelexecution/MediaTranscoder.java

Foreground service killed when user swypes away

As the title says I am having the following problem. My foreground service is being killed when the activity that started it is swyped away from recent tasks list.
I am starting a service with
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(notificationID, notification);
return START_STICKY;
}
Can someone please tell me why is this happening and how can I make it so the service stays running when user swypes the activity away.
I don't have access to public void onTaskRemoved (Intent rootIntent) for some reason but I don't know what to do in that method anyway...
I am starting the service like this this and it's not a bound service
serviceIntent = new Intent(this, RecordingService.class);
startService(serviceIntent);
If little use case description helps I am trying to control sound recorder from a remote view in the notification bar so restarting a service is not an option since it should continue to record even if activity is destroyed.
BTW.I did tried starting a service in another process by android:process=":recordingProcess" and the service does continue to run then but I am suspecting this is not how you should do it.
Even i had the same issue and i had access to onTaskRemoved() function.Please check this link, "Process life cycle" topic.
Try to return from onStartCommand() START_REDELIVER_INTENT, service will get start again.
From Android Developer Reference
A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these situations
hold, the service's onDestroy() method is called and the service is
effectively terminated.
How are you starting your service?

android and alarmManager

I write application which will use alarm manager. First I set up alarm manager to run some service every 20sec. Then service start some Thread. Here code of my service:
public int onStartCommand(Intent intent, int flags, int startId) {
ExtendedLog.i(TAG, "On start command");
Thread t = new Thread(wat);
t.start;
this.stopSelf();
return START_STICKY;
}
My problme is that this running properly only for few minutes. After that alarm manager starts service but thread does not start. I really dont know where is problem. Do anyone of you know what may be wrong with it? Thanks for any help.
Why are you stopping the service if you want the thread to keep running?
Looking in the Service documentation:
All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy().
So since you did not stop your threads in onDestroy as requested , the system probably interrupts and stops them on its own.
What are you trying to do here? Starting a new thread & new service each 20 seconds is probably (I might even say obviously) not the best way to implement whatever you need to do... What is the code the runnable wat runs?

Stopping an IntentService

Does anyone know if there is a way of stopping an IntentService without it finishing its work thread and stopping itself?
Simple question, but I couldn't find the answer in the documentation. Is there a simple way of stopping it?
Thanks
bevor a message to a service is enqueued onStartCommand is called. which forwards the message for queueing. so you could just override onStartCommand, something like that:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals("Stop"))
stopSelf();
onStart(intent, startId);
return START_NOT_STICKY;
}
cheers
You should be able to call stopSelf();
http://developer.android.com/reference/android/app/Service.html#stopSelf()
I currently stumble upon this requierement for an app i am working on. I will try using onStartCommand to send a message to the Intent Service to stop working (for example, setup a boolean flag stopWork = true) and evaluate it during the working job or before the next queued task. The IntentService wont stop inmediately but will skip all pending tasks. Hope it helps. Gonna try it myself also.

Categories

Resources