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.
Related
I've asked a question about keeping service alive but I didn't find the solution so I have another simpler question.
android doc says if android kills a service with START_STICKY on return of onStartCommand in low memory state, it will recreate the service if I'm correct.
but this service gets killed and disappear in running tasks after a period of time but it didn't get recreated! I run this service in android 4.4.2 on my phone, when screen is on, it survived about 20 minutes
but when screen is off it disappeared after about 3 or 4 minutes... on my tablet (again android 4.4.2) it stayed longer, about 4 or 5 hours and then got disappeared again (I got different results on different tests). I even test it on android 5 and the result was similar to tablet with android 4.4.2
am I missing something here? I thought service wont get destroyed when we are using return START_STICKY until I call stopService
here's my service:
public class MyService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
super.onDestroy();
}
}
sry for bad english :)
May be useful for someone--
This problem has nothing to do with devices with AOSP based ROMs.So android 4.4.2 version is not an issue.
So there are some devices (HUAWEI,LAVA,XIAOMI) are shipped with pre-installed start managers or energy savers, and they run on customized android ROMs. so these devices generally dont entertain a sticky service.
So possible option is to implement something like watchdog timer and check the service in between, if not started, the service can be run again. Possible implications may be on battery consumption though.
The service does get re-created, not re-started.
If you override the onCreate and do a Log.d or a Toast, you will see that onCreate gets called after your activity and app is destroyed.
So the trick to keep it running after it is re-created is to do your code on the onCreate method and use the onStartCommand just to return START_STICKY.
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
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
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?
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.