SQL query every 30 minutes triggers Android notification - android

I already have a WebView application connecting clients to a mobile friendly website. For admins, I would like to have a functionality that will run a SQL query (that I already have) every 30 minutes. If it returns true I want a notification to be triggered.
I have done some research and have downloaded a JDBC driver to connect to my MsSql database.
I would like to know the best way to have the app run the query every 30 minutes and conditionally trigger a notification.
Any tips or help is appreciated.
Please let me know if this is a duplicate, I did search before posting.
TBG

Use an AlarmManager which allows you to schedule your application to be run at some point in the future, in your case every 30 minutes.
Make sure you run your heavy query in the background using AsyncTask or something like that.
Check this to learn more about AlarmManager https://developer.android.com/training/scheduling/alarms.html and how to use it.

Related

Is there a option or other solution for quick intervals in WorkManager?

In my app, I want a worker to request my server every 1 minute using ( PeriodicWorkRequest ) whether there are new messages for the user.
The transmission is therefore kept very small and always contains the same json object that contains the important values. This looks like this:
{"1":**7**,"2":**0**,"3":**215**,"4":**0**}
In the WorkManager documentation, a minimum period of 15 minutes is specified for the periodic request. but not every app uses "firebase" and still gets all messages directly.
especially the server query is important when the app is closed. Are there any exceptions or other alternatives?
I've been looking for a lot but haven't found a solution so far and hope someone can help me.

How to fetch data from the database every day at 9 am in Android?

I'm creating an event app for Android. I am using a local database with the SQL Server and I have some questions about how to implement a background service that retrieves the most recent events from my database. Basically, I wanted something like this: every day, at 9 am, I get a notification that new events have been added. For that, I thought about using a background service and the alarm manager that every day at 9 am called the service that would look for recent events in my database. But I have some doubts on how I could do this? I saw some things on the internet but I ended up getting confused. And if there was no internet, was it possible to access the database as soon as there was a connection?
Thanks to anyone who to help me.
What you are looking for is Workermanager. https://developer.android.com/topic/libraries/architecture/workmanager/basics Simplified: You define a function which is called when the constraints you set are met. these constraints are 9 am and internet available and repeat every 24h.
I would use a PeriodicworkRequest for your task and set the time, when the first execution should be done with an initialdelay which you can define dynamic with some code, depending on the current time.
You can as you wrote in a comment do the work with a OneTimeWork which also triggers another OneTimeWork. The Request is only executed when all constraints are met and is postponed until they are met.
It is a matter of how time sensitive your case is, wether you use OneTimeWork or a PeriodicWork
This article could help you find your preferred solution
https://medium.com/androiddevelopers/workmanager-periodicity-ff35185ff006

Data exchange between Activities, Asynctasks and Services

Regarding the problematic stated below I have come to a point where I need to make a decision on whether to:
Start a Service once that has an AlarmManager inside which then starts the query every 10 minutes. This Service will only be stopped if the user sets an "Onn-Off" Switch to "Off".
Use an AlarmManager to start an IntentService every 10 Minutes. This Service will then only be started when needed and closed afterwards
Which of these ways is better when it comes to:
- Ability to exchange data received by the Service (Or Intenservice) with other activities/services
- Battery usage
- Overall "good coding habits" ?
Thanks!
Original Question:
I am a pretty new Android Developer and have come across a situation that I do not know how to solve. I have already spent several days searching for a solution but could not find one.
While trying to develop my first app idea I have started playing around with receiving and parsing data from the internet. What I have achieved so far is generating a query that receives JSON data via an API and parses this JSON. All of which is done inside an AsyncTask. The received data is then shown on the screen.
However, for the purpose of my app idea, I need this to be done in the background. What I have thought of is:
Starting a Service that pretty much has the same logic as my Asynctask. Managed by an AlarmManager, this service then requests, receives and parses the data in a specific time interval.
Now the tricky part begins:
The data that I receive (let's say every 10 minutes) shall be used to change an alarm clock. So, as a simple example, let's say the user can set his alarm clock to 08:00 in the morning. The application then checks the current temperature every 10 minutes and changes the alarm clock time to 07:45 if the temperature is below 0° celcius because the user has to wake up earlier to clear the ice off his car.
Also, when "waking up" the application, the current (or rather the latest received) tempereture shall be shown in the UI.
What would be the best way to achieve this? I am having some issues regarding passing/receiving data from AsyncTasks/Services to/from Activities.
My first approach would be to start a single service from the MainActivity, passing some data to the Service (like the initial time the alarm shall start and the current location of the user). The Service then has two seperate AlarmManagers. One of which is set to perform the actual alarm (waking up the user in the morning) and the other manages the time interval of getting the data from the internet.
My questions:
- Does my train of thought make any sense at all so far?
- What is the best way to pass and receive data to/from a service? My best guess would be to use intents to pass and a broadcastreceiver to receive data from the service. would this make sense in this specific situation?
I fear that it is not welcomed to post questions without putting in any effort of your own before. Although I did not add any actual source code, I hope you can see that I have dealt with these questions for quite a while now but could not really start coding before I know the structure of the application.
Thanks in advance
Use AlarmManager to start an IntentService as often as necessary (in your example, it should be sufficient to start checking the temperature about two hours before the user plans to get up and maybe again after one hour and finally half an hour before the normal wakeup time. More often only in case of extreme weather conditions.
It's not necessary to check the temperature exactly at 03:33 a.m. so use
setInexactRepeating(), this will be easier on the battery.
See also Scheduling Repeating Alarms
Write the results to SharedPreferences and have one IntentService check 15 minutes before normal wakeup time if the user should get up right then. Cancel the normal wakeup alarm in this case. Communicating via SharedPreferences (think of a mailbox) and local (!) Broadcasts is a good idea - cheap and secure :)

Fetching user location in background and uploading it to web server (trying to avoid using Service)

My application is based on Google Map and Location which includes client-server communications. Apart from all that, periodically I want to update the user location to web server. I gave thought myself and ended up with below two scenarios,
First One :
I use Service class to fetch user location periodically (considering 2-3 hours once) and update to server. Ideally I want to avoid this! Because just to upload user location its not good to run the service all the time is what I feel.
Second Thought :
I can use IntentService to fetch the user location periodically (considering same 2-3 hours) and update to web server. Here IntentService will be invoked by an AlarmManager. That is every 2-3 hours once alarm manager will start IntentService.
But I feel using AlarmManager for 2-3 hours once is not a good idea too.
I want to understand which one will be efficient and more relevant! Whether this scenario can be implemented in any other approach? If so help me do it!
Thanks in advance...

Periodic background check for a very simple task (Android)

I'm currently making an app in Android that is checking an API which returns two things. Some text and a colour.
However I want this to be checked for updates every 15 minutes in the background and check every 5 seconds when the app is open. When running in the background it should give a notification if the status is changed.
Now I have checked numerous stackoverflow q&a's and forums, docs etc.. But I can't seem to find a good baseline for what I need. So many documentation that contradicts eachother.. I think that I need an Alarm Manager or a Service... but what do you guys suggest for my problem? The app may not harm the battery too much.
What I really would like to have is that the application doesn't have to "poll" the server every 15 minutes but that the application gets interrupted like.. "hey, there is a new status update". I can't imagine that messaging apps are constantly polling a server for updates? I haven't found much information about that topic... Any help is appreciated. Not asking for code but directions to get where I want to go.
Many thanks
If you're looking to poll the server every X seconds/minutes, AlarmManager(android guide, tutorial) is exactly what you need. However, as you point out this is probably not the best way to go about things. While the app is open you may want to look in to passing messages between the device and server via an open Web Socket. Once your app is closed you could, instead of the app polling the server, have the server push a notification, via GCM or some such, to the app when an update is available.
If you are doing both the server side project and the mobile application, You can use Any messaging service rather than polling for the server, Because there has to be a pusher implementation from the server side to push the status to the MS.
For now GMS is free, I hope it will remain the same :). Otherwise, You can use AlarmManager and IntentService to achieve your goal.

Categories

Resources