How to track system's clock? - android

Situation:
I need to create a program that shows current date and updates some date(that is NOT CURRENT date,it is past date) according to current time(for example after 23:59,date refreshs). In other words,it is simple application,that shows date in the past and time.
What did I try?
I have found TextClock for this. It can show current time,but I cannot track over it.
So what can I do? I need to create my own clock class or use Timer and TimerTask? Do I need a Service?

I think, if I understand you correctly, you're on the right path. I wasn't familiar that there was even a TextClock view, but it looks pretty basic and will just work for displaying the current time.
Instead, maybe use a TimerTask to update the UI every second, or so, and set one TextView with the current time and another with your other time that's an offset of the current time (current = new Calendar(); current.add(Calendar.MINUTE, 1); )
You could use TimerTask, and that'd be pretty easy compared to rolling your own thread or using an AsyncTask. Get the current time, set it to one text view, manipulate that time variable and set it to the other text view.

Related

Realm Flowable query filtering by the current date

Context
I'm working on an offline first app, and I'm basically working on a screen that is supposed to show a list of store offers - discounts, coupons, things like that - I'm trying to rely on Realm queries via Flowables to fetch the data, and I want the app to be 100% honest with what we have on the Realm DB. I mean, that if something changes in the database the app should immediately refresh the UI, that's why I'm using Flowables.
Problem
This approach has been working fine so far, but I've stumbled across this specific situation where things are a bit tricky. I need to display this list of store offers like this:
private val offersFlowable: Flowable<RealmResults<RealmOffer>>
get() {
val realm = Realm.getDefaultInstance()
return realm.where(RealmOffer::class.java)
.equalTo("userId", CredentialManager.getInstance().getUserId())
.and()
.beginGroup()
.isNotNull("expiryDate").and().greaterThan("expiryDate", Date())
.endGroup()
.findAll()
.asFlowable()
}
The query itself works perfectly, I fetch the user's offers which haven't expired yet. The only problem is that it seems I need to recreate this query every time I want to refresh the data. Why? Because of the Date(). I open the Activity that renders this RecyclerView list of store offers, I instantiate the query and start listening for the changes - so far so good - but if I have a store offer that is about to expire in a minute or so, wait that full minute and do a Pull To Refresh, the expire offer doesn't go away. Again, because of the Date() because the current date was instantiated a minute ago and it is still that date.
If I kill the app or instantiate the Activity from scratch, everything works as expected because I'm instantiating the query again, therefore getting an up to date Date().
What I've tried so far
Instead of hardcoding that Date() arg there, having a currentDate property in my ViewModel - the place where this Flowable lives - I was kinda hoping that if the date arg was a reference to a computed Kotlin property that would do the trick, but it didn't work.
Same thing as above but I changed the expiryDate field to millis instead of a Date.
The Workaround
So, the only thing that works right now is just re-instantiating the Flowable query every time I do a PTR, that way I pass an up to date Date() arg and the query works as expected.
My Question
Is there any way to fix this without having to re-instantiate the query all the time?
Thanks! Appreciate all feedback! 🙇
I hope that I have understood your problem right, I would like to put a suggestion may be that help you. The current work around for sure is not a good option because of requesting the database again and again. so what in my opinion would be better for this situation is,
Before rendering the data on the RecyclerView, Do the following;
1 - get the expiry date & time of each item.
2 - get current date & time.
3 - Compare both of them and get the difference.
Now, if the difference is -ve value, it means the item is expired and you don't have to show it in recyclerView. Of course the item is available in ArrayList/List but it is not being rendered on recyclerView.
Otherwise Second Option
At the time of adding the store offers data to the database, just register a PendingIntent for every offer along with an AlarmManager and set the Expriy-time as the alarm time for the PendingIntent.
This way, when the offer expires, it will throw an alarm. So, here you could do the magic, when the PendingIntent get trigger, just update your database and recyclerview from there. This way you can handle every offer automatically.
Let me know if it helps.
Happy coding :)

update textview from couchdb using changelistener android

I am trying to get real time changes on a document using the changelistener, however i have to refresh tha mobile app to see the change. Here is my gist https://gist.github.com/mikekaraa/89416ea8b074c71d7153
Please help me out if you have a solution
Would you tie your ChangeListener() event to a time interval such that e.g after every 3 or 5 seconds it is fired and triggers the next sequence of events if at all the document changed within that time span.
I hope that helps.

Android ListView don't update element's view

I'm developing a custom ListView, which uses a custom ArrayAdapter and custom elements.
Actually I'm displaying a list of tweets.
I want to display for every tweet the time elapsed from the creation, exactly like twitter.
I wrote a timer that calls a method on every list element. This method calculate the elapsed time and set it on a TextView on the element with setText().
Problem is that I can't get the list update. The values change only when I add a new element or manually scroll the list.
If you need code ask freely.
EDIT:
The answer of Nick Caballero is correct, but it wasn't working.
I have already tried that code. The problem was in the timer and in a try catch with a generic Exception.
The timer was trowing a CalledFromWrongThreadException. The solution was to use a runOnUiThread for its operations.
Calling notifyDataSetChanged does additional operations that are not necessary here. Updating the view with the time elapsed relative to the timestamp of the data does not imply a change in the data.
See https://stackoverflow.com/a/2679284/724068 for how to loop through the visible views in the list. You can then extract the view containing the time elapsed and update accordingly. Since you have to run this every so often to keep the UI up-to-date, you will have to use a Runnable, posting to the main Looper every second.
Also see https://stackoverflow.com/a/9987616/724068.
You might want to try calling invalidate() on the control once its updated. Code would be helpful though.

Android: Timpicker Difference declared in xml and in java code (TimePickerDialog)

So i have this problem of not getting accurate time result from timepicker declared in xml, not the dialog thing, i just wanted to know what is the difference from the two. so that i can know which is better to use on my app, mainly i use the timepicker to set an alarm or a reminder, because i have a hunch that the timpicker declared in xml is just for UI use not really for functionality. because i keep on getting the wrong result, specially dealing with AM and PM, Scenario is that when using timepicker declared in xml (not the dialog), i change first the hour den minutes and lastly the AM and PM, but i won't correctly give me the current time, say i chose 7:30 PM supposedly the value of the hour must be 19 since its 24-hour but instead it gives me 7:30 AM i won't change the AM and PM part thus giving me a wrong result, if you go the other way around, it works, it gives me ang accurate result.
SO my question is what is the MAIN difference between the two timepickers, is it for show(timepicker xml declared) and timepicker(dialog)
Please refer to this post for this problem, i just came across this thread and it is really the same with my problem. its a bug issue in Android
Android TimePicker AM/PM button not invoking onTimeChanged

Android Chronometer

How to start the chronometer with a specific time other than default 00:00? Is it possible to set chronometerObj.setBase(startTime) ?
ch.setBase(SystemClock.elapsedRealtime()-anylongvalue);
ch.start(); can I set start time, if I put anylongvalue?
In general:
mChronometer.setBase(SystemClock.elapsedRealtime() - (nr_of_min * 60000 + nr_of_sec * 1000)))
Chronometer object, when instantiated, defaults to the base time being set to now ('now' as in the value you get from SystemClock.elapsedRealtime()).
You can change the base time (00:00 time) by calling setBase(<some other value>).
Presumably, although I haven't tried the experiment, you could see the elapsed time since last system boot using setBase(0).
So you can use chronometer to see the elapsed time since any arbitrary call you made in the past to SystemClock.elapsedRealtime(). The trick is that you need to have stored that value somewhere you can dependably get it back despite app and phone state changes. (See Android: chronometer as a persistent stopwatch. How to set starting time? What is Chronometer "Base"? for example.)
Many answers suggest persisting that arbitrary time-in-the-past in an intent. but, at best, this only keeps the timer counting up while the phone stays on.
I already am using a database and store my starting time in there. I created a one-column table for it and store a single record in it. My starting time for the chronometer survives a phone reboot.

Categories

Resources