Background Process, no user interaction - android - java

I have an app in Android where the user can refresh his data manually (Example: Consumed sms, Total Balance ... ).
However I need a new feature:
- Implement a schedulle or alarm, where the data of the user will be refreshed every morning without user interaction (Background Process)
I tried with Services class... But I have not clear yet
Thanks

Related

Creating Notifications for Task Management App (Java & Firebase)

I want to create a notification system for my Task Management Application Which Stores the Task Title, Description, Time and Date (As Strings) on Firebase. I tried several methods Available on YouTube but I was only able to Trigger the notification but the Title and Description are not being sent to the Notification Channel which ends into sending an Empty Notification and I want to create notification for certain date and time too. The Triggered Notifications were only triggered when I manually set the time in code and not when I set the time and date from App.
(The method I tried worked only for Android 10)
I want to get the Task Title and Description and Trigger the notification at the time and date set by the user.

Perform an Action when the app comes to foreground

I am developing an app which has to perform some background work. For that reason, I am using a Service. Now what I want to achieve suppose the user start the background work and while the work is loading then the user has an option to either minizine the app or wait till the loading is over.
After the loading is over I want to open another Activity. Now my issue supposes the user starts the loading and minimizes the app then when the loading is over the user has not yet returned to the app then if I start the Screen without even the user having my app in his view then the user might get interrupted with his work.
So what I want is when the loading is over, I want to only open if my app is visible to the user and if the app is not visible to the user then I want to wait till the user return back and only when the user returns back I want to open the Screen if the loading is over.
Now what I have thought is I should have a boolean which will track whether the app is visible to the user. On onStop I will set the boolean value to false and onStart I will set the value to true. And again onStart I will check if the loading is finished and if yes then I will open the Screen.
But I want to know whether there is a better way to achieve this? If yes then how. The reason I am looking for a better way is that I want to write a clean code for my app which might avoid bugs and crashes.
That is exactly the scenario LiveData and RxJava are for. Your activity will get the data only when the activity is visible. Your Viwemodel will provide your live data to the activity only when your activity is available and it's lifecycle aware. You can also consider using WorkManager if your app needs to continue to work even after your user closed your app, even if user restarts your app. It also comes with Constraints to optimize your work based on Network, Battery life...and provides livedata for your Viewmodel to consume.

Checking for new data arrival from a RSS feed

I made a RSS reader app for android and it gets the latest news from a RSS feed. I want to show a Notification to the user when a news is published to the website or the RSS feed. How can I check for new data?
You can create something which is called polling. Actually many rss reader apps using polling technique in which their
service wakes up at a regular interval to contact a server to see if new data needs to be processed.
This means that the application needs to wake up to look for new data even if none is
available to be processed. Read about IntentService and AlarmManager. Basically you can create AlarmManager which is going to start every amount of time that you want for example every hour. Than you can start a service which is going to check is new data available. If it is new data for processed available raise a notification on user phone. Set AlarmManager to fires up again for an hour and the service should shutt down itself. Hope this will help.

Background Service that makes an HTTP Request every 15 Mins

I am trying to implement a background service that makes an HTTP request to an API every 15mins for the whole day, starting when a certain activity is started. I need the service to be started every 15 minutes even if my application is not running or I am in another activity of that application. I've searched for an example for how to proceed an have looked at some Stackoverflow questions and answers for example: Start Android Service after every 5 minutes. which linked to this page : http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/
From the discussion I know that I need to use an alarm manager, however all the implementations do not have this being done in the same activity they have broadcast receiver. The reason I would want to have the implementation in the same activity is because the results of the request is what I want to display on the UI.
Is there a way of implementing an alarm manager in this type of situation
however all the implementations do not have this being done in the same activity they have broadcast receiver
That is because that is your only viable option, and even that will not work well on Android 6.0+. The recipe for using AlarmManager for this sort of scenario is to have it start a WakefulBroadcastReceiver, which in turn will work with an IntentService to do the work and go away when the work is completed.
On Android 6.0+, courtesy of "Doze mode", your AlarmManager events will not fire every 15 minutes, if the device is not charging and not moving. Also, courtesy of "app standby" on Android 6.0+, your AlarmManager events will not fire every 15 minutes, if the user has not been in your application's UI for some time and the device is not charging.
The reason I would want to have the implementation in the same activity is because the results of the request is what I want to display on the UI.
This runs counter to an earlier statement that you made:
I need the service to be started every 15 minutes even if my application is not running
If your application is not running, then you do not have an activity.
You are welcome to have your IntentService post a message on an event bus, such as greenrobot's EventBus, to let your activity know about the results of the work... if the activity happens to be around (otherwise, the message will be ignored).

Saving large amounts of data when onPause is called?

For my app, I have about 15Mb (which can be compressed with some processing power to about 5Mb) of audio+video+image data that I need to save to disk. For instance, I need to save this when user is interrupted with a phone call (because the activity might get killed after this) and when the user leaves the app.
I can save the data to SD card in about 10 seconds if I don't compress it and something like 20 seconds if I do compress it, where I'd like it compressed. What options to I have for saving my data when onPause is called such that I can be sure the data has been saved?
From some basic experiments, my activity gets killed if onPause hasn't finished after 5 seconds. Some ideas I've had:
Starting a new Thread in onPause and saving the data there. This seems to work fine but seems like something I shouldn't be doing.
Starting a service, copying the data to the service somehow (would this be slow?) and then getting the service to save the data. I think this puts a notification icon at the top of the phone, but I don't think it's awful for a user to see the "Saving data..." task here.
Can I put the data in a SQL database quickly and then save it later when the user returns to the app?
(Due to the nature of the app, there really isn't any practical way I can save the data as I go because the user can transform the data in destructive ways with time consuming operations (e.g. 10 seconds for some operations). Even if I stored the original data and a list of the actions performed to recreate the data, the user would have to wait a minute or two when the app is next started up to process this.)
What options to I have for saving my data when onPause is called such that I can be sure the data has been saved?
Technically, what you want is impossible. There are no guarantees after onPause().
The best answer is what #Viktor Lannér suggested. To phrase it another way, don't wait until onPause() to need to do 10-20 seconds of I/O. Devise some mechanism to allow you to save incrementally as the user performs operations, as a fallback mechanism if nothing else. This is akin to how a database maintains a transaction log.
Starting a new Thread in onPause and saving the data there. This seems to work fine but seems like something I shouldn't be doing.
This is dangerous, because if the activity is closing (e.g., onDestroy() will be called momentarily), Android might terminate your process before your thread is completed.
Starting a service, copying the data to the service somehow (would this be slow?) and then getting the service to save the data. I think this puts a notification icon at the top of the phone, but I don't think it's awful for a user to see the "Saving data..." task here.
Make this be an IntentService, so it automatically shuts down when the work is complete. I wouldn't "copy the data to the service", but rather make the data centrally available, by a static data member if needed. This will not automatically put "a notification icon at the top of the phone", and for something of this duration, that is probably not needed.
Can I put the data in a SQL database quickly and then save it later when the user returns to the app?
Flash I/O is not faster for a SQL database than for anything else.
Due to the nature of the app, there really isn't any practical way I can save the data as I go because the user can transform the data in destructive ways with time consuming operations (e.g. 10 seconds for some operations
Then this probably isn't designed for a mobile platform. Consider whether this app is an appropriate use of the technology.

Categories

Resources