Do something at a certain time of day in an android app - android

I'm writing a reminder app for Android where users can create custom notifications that will appear at a time of the day that they specify. I realize that there is another similar question, but the only answer was to use the AlarmManager, which, according to the documentation, resets when the device is rebooted. I want the user to be able to set a notification to appear at a certain time on a certain date, meaning that it should retain the information through a reboot.
Ultimately though, the app needs to be notified that it needs to do something.
Eventually, I would also like to have the same behavior when the device reaches a certain location, if you have any thoughts on that. :)
Bear with me, I'm a bit of a novice programmer (I'm 17).
Thanks in advance.

The commentors are correct: you will need to use an AlarmManager to create your alarm and you will need a boot receiver to handle resetting your alarm after a reboot.
GeoLocation and GeoFences are pretty easy. I have an example app here: https://github.com/androidfu/GeofenceExample
You're not going to care about the mock location bits in that example unless you wish to test entering and exiting your target location, but the rest of the code should work for what you need.
Also, GeoFences do not persist a reboot either so it'll be good for you to get familiar with your on-boot receiver ;) You'll want to re-add your GeoFence after a reboot too.

Related

Updating time of clock widget

I am looking at creating a clock widget that tell the current time but if the minimum time you can set for the widget is 15 minutes thats obviously not going to work.
I also see suggestions on using AlarmManager but then I worry about the battery life.
I also see the intent action Intent.ACTION_TIME_TICK that fires every minute and seems like what I want but as the documentation says
You cannot receive this through components declared in manifests, only
by explicitly registering
So that seems like its not going to work for a widget
So the question is, is there a "correct" way to build a clock widget that does not kill the battery and still updates the current time?
If you are talking about a Digital clock , Android added a TextClock layout widget on API level 17 .
you can simply add it to your widget XML and it will automatically show the time.
Example:
<android.widget.TextClock
android:id="#+id/clock"
android:format24Hour="k:mm"
android:format12Hour="k:mm"
android:gravity="center" />
I actually just made (this week) an open source android digital clock widget , feel free to browse and use my code. pull requests will be much appreciated!
I'm sure it will be helpful to you since I stumbled a bunch of surprising walls on the way.
https://github.com/y0av/MaterialClockWidget
if you want anything other then a digital clock you will need to use an AlarmManager like you mentioned.
Make it tick only when the user is watching. So in onResume make it to current time and update it every minute or something, then in onPause stop updating it and kill the timer,handle,rxInterval or whatever you are using to update the view periodically.

Foreground or Background service to track user's location?

I'm developing a location aware app. This app will start tracking users when they are in their workday. These are the requirements:
The service should run on a regular basis (every 30 or 45 min).
It won't matter if the service does not trigger at the same basis everytime.
The service needs to upload data to our firestore db.
I doesn't need to be triggered on specific conditions (data is on, phone is charging, etc...)
I need this to run even if the user restarts his phone.
We may need to track the distance traveled by the user. (This is not a requirement per se, but it may be a feature someday)
I know there are a lot of ways to achieve this, but I have not decided which is the option that best fits my scenario. This is what I've thought so far:
Foreground service combined with BroadcastReciever in case the phone is rebooted
Background service using the new jetpack's Workmanager.
Which will the best solution? Do you think there is a better solution than those?
Thanks!
Was thinking create a GPS location tracker so when they are in work premise as well as outside it kinda shows.
Then consider adding the number 5 of the above. Like you said there could be mire awesome solutions than these so lets wait for options.

Android Clock which runs when device off

I am creating an application, which will save the current time (with some delay eg. 2 hours) in file, when the user presses a button. Later on, the application will check if the time has passed and do some stuff...
So... I click button in application (time gets saved in file)... I quit application... shut-down phone... I turn it on after 1 hour, get back to application... and I will still have to wait 1 hour until the application will let me do "something"...
QUESTION:
Is there a clock that cannot be changed by the user and keeps running when the device is turned off? I'm currently using SystemClock.elapsedRealtime(), which works fine, because even if users change the time in settings, elapsedRealtime stays the same. The problem is if the device gets turned off, because at every boot elapsedRealtime starts with 0.
I cannot use server time because application will not be connected to Internet.
If there is no such clock, please suggest me another solution.
actualy, you have no chance to get "off" hardware clock data. hardware clocks was just on older phones in the new phones i think nobody need it so they dont build it in hardware. In the old phones there was "hardware" clock but in the new device is nothing like that i think. I did read something about that google want to make some framework or what to implement it. But there is no alarms what are able to start in off mode.
So i am sorry, but i think it is not possible right now..
You could store your time in a database as a DateTime value, indicating Year Day Month Hour Second Millisecond, then you could request for a service to start on boot and read that data creating an alarm that triggers in the remaining time. I would give you a code example, but i'm not really good at java programming so it may be useless, anyway goodluck and try to implement this.
You obviously need to save your data to non-volatile storage. When your app is paused/destroyed by the Android, you should take it as a threat and save your time values to the disks, and then when your app has started again your app should read the data you have written before and keep on running as it would normally.
Well when it comes to question how:
the simplest solution is to use SharedPreferences,
the more complicated and the more flexible one is SQLite Database,
for more data on Android storage I will suggest: Storage Options

Not receiving the broadcast while running the LocationPollerDemo

I've searched through a number of the questions and answers in regards to the LocPoller from Commonsware and haven't found the answer to my problem. I have tried using the demo of the latest from github and I'm having issues. First I must admit that I modified the PERIOD in the demo so the alarm fires every 15 seconds. I realize this will be a large drain on the batter and it's just for testing. I would expect that the LocationReceiver's onReceive() would be called ever 15 seconds but that isn't happening.
Debugging things I see that the alarm is triggering and everything seems good in that it's getting to the PollerThread and calling sendBroadcast. So I assume I have something setup wrong, but I'm not sure what. Other than changing the PERIOD in the demo everything else is as is. I've tried running this on a device and emulator (Level 8). Any help in debugging this would be appreciated.
In the end the functionality I'm looking for is to have the ability to keep track of a phones location so at any point my app can get the current location. I saw this in my research and though it fit the bill perfect. If someone has a better idea of different approach I'd love to hear it.
Thanks,
Rindress
In the end the functionality I'm looking for is to have the ability to keep track of a phones location so at any point my app can get the current location. I saw this in my research and though it fit the bill perfect.
No, it is not.
If someone has a better idea of different approach I'd love to hear it.
Step #1: Call requestLocationUpdates() when you want to start receiving location updates in your app
Step #2: Use the location fixes delivered to your LocationListener, or call getLastKnownLocation(), as needed
Step #3: Call removeUpdates() when you no longer want to receive location updates in your app

Android scheduling or timer APIme

Pardon me if m asking some discussion based questions. I am completely new to android environment and I am developing an android based application wherein i need to apply some modification in messaging system for my project. therein i need to employ a timer facility for the outgoing messages where user could be able to time his message so that at that scheduled time, the sms should go automatically. I want to know which messaging API will help me in implementing so. I can sense that dis is not upto the mark question to be asked but i searched on every site but all work in vain. Hoping to get a positive reply from your side.
You should have a look at ScheduledExecutorService
i think this is what you are searching for. This service is able to execute tasks based on TimeUnits in the background.
You might also want to check out AlarmManager. You can use it to send a custom Intent at some time in the future. It might be better than other kinds of timers (depending on your situation), because it will cause the phone to wake up when the intent is delivered. Otherwise, your code might not run if the CPU is asleep.

Categories

Resources