Get changes in content provider - android

I'm developing an application that syncs a remote calendar with an Android device.
Everything works fine, but now I want to make it a bit faster by letting the Android device only sync events that got edited by the user (with Android Calendar). So I need to know which events got edited.
I've been looking into ContentObserver but that doesn't seem like a good solution since I would have to register an observer for each event in the calendar. And I don't even know if the observer would still be there if I restarted the device.
Another solution I came up with is to store a copy of all events in a local database. That database would get updated on each sync. And before each sync we could compare that database with the Android Calendar provider...
Is there an easier way to do this?
Thanks,
Gillis

I'm pretty sure you could query the calendar database for events modified after a certain date (which would be the date after the most recently modified event was modified).
The query would be so cheap you could repeat it every second or so.
Edit: I've been looking through the schema for a column to represent the event last modified date and I don't see one. So maybe that's not a viable route.

Related

Accurate time without sim and without internet

I have an app which is used for collection all day users collecting amount normally offline and then in the evening they uploaded amount online. Sometimes there device date and time is inaccurate so they uploaded wrong data.
At the time of collection my app is getting date and time from the device. Is there any way that app always store accurate date and time without Sim and without the Internet.
You can create a Sqlite Table in application cache when user switch on data,you
can save current server time in that Table and in offline mode you can compare server time with current time. this is the nearest approach to get accurate time
Well, before searching for an app to do this. As a human how can you tell if the date/time is correct without internet?
A possible solution would be a service which counts seconds from the last time the device was connected to the internet (which means it had a correct date/time at that moment) then when you want to add the new amount you can add the seconds to the last time and store this value.
Edit
You will 2 things to achieve this:
BackgroundService
Counter which you can achieve in many ways, I would go the easy way. check this: how-to-measure-elapsed-time
Or you can have an AlarmManager to do this.
One more thing You need to be careful if the device goes off or for some reason, something stopped the service. All of these are user issues and you need to decide how much you want to handle stuff for the user.
You can fix user mistakes by pushing to a SqlLite database every 10~15, but this won't fix if the device went off for hours and then started again at a wrong time - I don't think there is a possible solution for this-.

Get datetime/timestamp from a website/url in android

here is what i'am trying to achieve, either one of the two would be ok
Get the date and time from a specific url/website like "www.mywebsite.com"
Get the date and time from my MSSQL Server
either one of the two would be ok, because all i need is to get the same time from the website/server's end, because i'am making an APP Version for the Mobile Website i'am making and i want to show a Clock or Date&Time Reminder in the App
i have no codes working as of now, i tried searching online in search engines and also in stackoverflow but i can't seem to find what i'am looking for, either i'am using the wrong words to search for it or there really isn't any way
the reason why i need the date is because i'am checking when the records inside the database was created and verifying how much time has passed since it was created, it like comparing (CreationDateTime & CurrentDateTime)
i can successfully Display how many hours have passed, i just need the CURRENT PROPER & ACCURATE DATE & TIME of the server :)
I can't help but think this is an XY problem, or at least close to it.
Your client device knows what time it is, because it stays in sync via NTP and whatever network it's attached to (and we already know you're attached to a network because otherwise, this whole thing falls apart). And it knows (or should know) what timezone it's in, so you can translate that to UTC or whatever alternative timezone you might need.
Your server(s) should also be keeping time synced via NTP, so drift between the client and server should be sub-second at worst.
IOW, the time (when corrected to UTC, which all date/time libraries I've used have a method built in to do) will almost always match between your client and server, so why bother with the complexity of "pushing" the current time from server to client?
I'm not seeing why you'd need to do this at all. Have the server tell the client when the reminder is set for, then let the client work out the rest. Anything else would be excessively chatty over the network.
To get the date in the format below, just convert todays date to a string
--07/06/2017
SELECT convert(varchar(10), getdate(), 103)
i have found an alternative answer that works 100% in getting date and time from SERVER instead of URL
SELECT GETDATE() AS CurrentDateAndTime
GETDATE()

syncing between phone and web algorithm

i have a simple note application in two platform, One in mobile(Android & iOS) and other one is web based. I want that this two sync.
My problem is that: How i can determine that which version of a note(android or web) is newer and that text has to be sync? In fact, I need a sync algorithm. If i use timestamp, if users date time in his phone goes to wrong date time, syncing system throws to problem. I want newest edit syncs to all clients.
For example if mobile app were offline and make some changes, In the same time we made changes in server in the same note and when mobile App goes online, which of this changes should be kept? What is the solution?
In the end, What is the best method for this? I want to have best sync method
Clients shouldn't use their local time. Only the server has the right time. Clients have timezone which they can use to show appropriate time to the user. Roughly speaking sync algorithm could look like this:
Client A edits a X note and sends event(request) to the server that the note was updated.
The server receives event and updates timestamp of the message. Then it broadcasts new event that the note X was updated
Client B receives event (with updated note and timestamp) from server and shows to the user updated note with appropriate time.

How to change data on a specific calendar day

I have an app on Android with a DataBase. This database has tables with ChangeDate columns, where I keep dates when some actions need to happen. For example, 2014-03-17..2014-05-18 and etc. The problem for me is that if I'm comparing for about 200 db records with today date on the app start , it will make my app heavier.
Is there another way or maybe a type of trigger for such things? Maybe you can advise something. Thanks in advance
Checking against 200 dates will take an instant so do not worry about the load for such a small number.
For other solutions, it depends on what those actions are. If this is just some actions to take when the app start, then the solution you suggest seems perfectly fine.
An other option may be to load your actions when loading and submit them to a ScheduledExecutorService.

Retrieving the current date and time from a time server

In my Android app, I would like to retrieve the current date/time (UTC) from some server. Android does have the built-in ability to synchronize the device's date and time with some server but I have found this to be unreliable. Does Google or some other well known service provider provide a simple API that would allow me to retrieve the current date/time? I am not interested in trying to set the date/time on the device (that's not even possible). But I do need to know the correct date in order to perform scheduled tasks that depend on exact dates.
I have seen a device reset its date to something like 1980 when the battery has been removed for an extended period of time and therefore I cannot rely upon the date of the device.
EDIT:
I came across this post:
https://stackoverflow.com/a/13066268/753632
But it requires installing Apache Commons Net. Kind of overkill just to get the time. I don't need to bloat my code more than it already is.
you can refer this link How can I get the "network" time, (from the "Automatic" setting called "Use network-provided values"), NOT the time on the phone?
You could use the calender class, like is explained here, although this uses the time of the device itself. You could also read this it explains how to get the time for the service provider.
I think using the calender and then calculating the UTC time with based on the timezone the phone is in (like this). Because the phone already gets it's time from a server when it is connected to the internet.
I hope this helps.
EDIT: I see you edited your post. So you don't want to use the device time... I think you could look into getting it from the service provider then.

Categories

Resources