Scheduled Execution of Process - android

As a requirement in Android OS phone, I am developing an app which will clean up a particular folder in a specified interval of time, say every 30 minutes.
I have tried this using services, check my other posting stackoverflow: file-cleaning-service
I am trying to find a best method to execute the file cleaning operation.
Should I use a service with broadcasting (or a Handler)?
Should I perform it within the app without using a service? The app can hide itself and perform task in the background. Is this approach is good? My point is why should I use a service if the service is just sleeping for a specified time, why same cannot be achieved in the app itself?
Is there any other method through which I can achieve this?
I do not want to wake up the screen, hence I do not want to use AlarmManager.
So please help me with best solution. Thanks in advance.

Related

Android - how to overcome the 10 second limit when receiving broadcasts from IntentService?

I would like to know, I have an Activity that receives broadcasts from an IntentService that needs to save data and then export it to a CSV file. However, this can take longer than 10 seconds, and I know there are certain problems with starting AsyncTasks and Threads using BroadcastReceivers that they might get killed after 10 seconds. I would like to know, how would I overcome this?
EDIT: Note that what I am actually doing is saving data from sensors, calculating data, and then exporting. Apparently when the app is in the stopped state when receiving the save command from the other app, the app gets stuck in the calculation state. The save and export use AsyncTasks, but the calculation uses a Thread where I send a message back to the Activity using a Handler. Does anyone know how I would get around this? I used PendingResult in the saving portion, but now it gets stuck in the calculation portion.
Have it launch a Service to do the work, rather than doing it in the Receiver itself.
Here are different approaches to save data in background.
Android background processing
It really depends upon your requirements. If exporting to csv can tolerate a little bit delay, use WorkManager as its battery friendly and automatically uses best approach depending upon API level.
Use foreground service, if you want immediate high priority background execution and you also wanna notify user because foreground services are visible to users via a non-dismissible notification in the notification tray. For example, WhatsApp attachment uploading.
If it is not a very long running background task and you don't expect user to close the app during this, you can use any background execution approach like ASyncTask, Rxjava, kotlin Coroutines.
background execution evolution. A very interesting article to read and compare these different approaches.
I figured out a solution. As suggested, I used a foreground service, however, there was an issue with my callback to my activity in my background thread that was started by the service that the callback interface was null when the app was paused. I was able to fix that.
Thank you for your help.

How to have a service to run eternally on Android?

Search engines and Android developer website didn't help and I guess you can help with my problem.
I want to make an app for personal use, which is supposed to run all the time on my old tablet (powered all the time). The app will have several features requiring user interaction but independent of those, it should run a background job to check something continuously (real time!) for instance sound detection. It should also always try to connect another device on the network.
That means that job needs to run almost eternally without being killed. Some comments I have found suggested AlarmManager or BroadcastReceiver. But those are triggered by very defined triggers (either time or broadcast). I don't want that, because it should perform its task continuously all the time. This background job should also be able to communicate with the main Activity of my app to report what it is doing and allow user to interact with it (change settings of the job for instance).
Do you know any way how to accomplish this? Is IntentService correct choice for this (hoping that it won't get killed or maybe I should let the Activity to restart it?)
Thanks!
Do you know any way how to accomplish this?
Build your own custom ROM, with a modified version of Android that contains your code as a native Linux daemon.
Otherwise, what you want is technically impossible.
You can come fairly close by using a foreground Service (not an IntentService) and returning START_STICKY or START_REDELIVER_INTENT from onStartCommand(). Android may terminate your process from time to time, but it should restart your service automatically after a short while. That service can use its own background threads to do whatever it is that you are trying to do.

Android stand alone service

I just started working on an application for Android, to be specific - a service.
I would like to create a service (background service, forcing itself to run, hidden, so it can't be shut down). and I don't have idea on how to construct it. How it is then launched and how to make it fit to those requirements. It needs to start running from the Android start, so I guess it would be created similarly to system services (or as a system service).
You may ask why I need that - I would like it to periodically send some data to MySQL DB without any user interaction - therefore it needs to run continuously.
It would be really helpful if You could give me a great start, a tutorial maybe or some structure drawing with crucial information.
One more thing - I am targetting Android 2.3, but if it's impossible, I'd like it to be the lowest possible Android version.
Thanks in advance guys!
I would like to create a service (background service, forcing itself to run, hidden, so it can't be shut down).
Fortunately, this is not possible. Users have control over their device, not you, and they can shut down whatever they want.
It needs to start running from the Android start, so I guess it would be created similarly to system services (or as a system service).
System services are part of the operating system and cannot be created via the Android SDK.
You may ask why I need that - I would like it to periodically send some data to MySQL DB without any user interaction - therefore it needs to run continuously.
It does not need "to run continuously". That would be a singularly poor implementation.
If you want to do something periodically, use AlarmManager for the scheduling. The work should be done by an IntentService, so the service can go away when the work is done, so you are not tying up memory all of the time and making it more likely that the user will shut you down. If you plan on waking up the device out of sleep mode to do the work, you should look into WakefulBroadcastReceiver, or possibly my WakefulIntentService.
Alright, I found the answer myself, but thanks for help to any of You who contributed :)
I am aware that it's not the best approach, but I need to do it that way, so I found this for running a service and restart after stopped, also found that for start the service/application on boot.
Both tested and worked for me.
I hope it is going to help anyone who needs it.
Do you want this service to be executed daily??
You need alarm manager .. that will help you in to start you service with android start and will execute your service after specified time delay.. You can specify the time for delay also. Dont forget to add permissions for alarm manager in manifest file.

How to poll for current time in a Service?

I did a search before asking so please don't tell me to do that.
I'm relatively new to Android so I get confused easily.
I'm making a simple app that changes the ringer volume according to time. For this purpose, I know I need a service that keeps running in the bg and monitor time. However, I really don't know how to approach this. I have the Professional Android Application Development book but haven't found anything that helps in there.
My question:
How to constantly check time without destroying the battery
If my code is needed (as proof of me actually working on this), I'll post.
You don't need a service. Use the AlarmManager class. Its like an alarm clock and it exactly what you need for this type of app.
need a service that keeps running in the bg and monitor time
No. Actually that's not how to do it. Services on android are different than your normal windows service/unix daemon. They should do their job and then stop themself until they get started again - to save battery.
You should start your service at a certain point in time by using the AlarmManager, it sends the launch intent to run the service. When the service is finished doing what it's supposed to do (change the rintone volume here), use Service.stopSelf() to kill it.

Background process to present notifications when main app isn't running

I'm implementing an background process that will update information my app uses.
I only want this process to update say once a day, if the process gets data newer than what it had before I want to present the user with a notification, exactly like twitter/gmail does.
I want the update process to run automatically, even when the main app is not open.
Is a Service the best way to go? I've been reading quite a bit about this, I figured a service running all the time for something that is only going to do work once a day seems a little overkill.
However I notice google run service for friendlocation and google+ services continuously on my nexus.
I've look into starting my service via the AlarmManager so its only started when required.
Some posts also suggest using the Handler class, I don't think this will work.
Just looking for the best practice here.
I figured a service running all the time for something that is only going to do work once a day seems a little overkill.
Absolutely.
I've look into starting my service via the AlarmManager so its only started when required.
This is the correct answer.
If you only want your code to be invoked if the device is on, implement an IntentService, do your work in its onHandleIntent(), and have AlarmManager start up the service on your desired schedule.
If you want your code to force the phone to wake up, you can do that, but you will need to use a _WAKEUP-style alarm, and you will probably want to look at my WakefulIntentService, designed to handle this pattern.

Categories

Resources