androidandroid-alarms

Android offline reliable date duration


I want to design an offline payment method where the user can use the app for duration of the payment only. I want the payment duration to be reliable even though the user changes their phones time/date.

I've tried using AlarmManager to periodically update remaining payment counter. But this doesn't work when the user sets the date to the past, I've tried to listen to DATE_CHANGE broadcast. But that also doesn't get broadcast when the date is set to the past.

I want to know what is the right way of implementing this type of functionality. Any advice is highly appreciated.

edit 1

The app is designed for places that don't have reliable internet. So most of the time, the app is used offline. So, I can't assume there will be internet connection to check the correct time.

edit 2

time date set broadcast not firing in the past

ASOP issue


Solution

  • To make it clear without Internet access there is no definite way to solve your problem. The only thing you can do is to monitor all time sources you have access to and compare them to detect manipulations (check regularly and record data).

    This will not make it impossible to use the app after the license has expired but it makes it harder.

    First time source: Device clock

    You can monitor it in regularly, e.g. using a CountDownTimer in a service. Record the UTC time (without time zone) in your app and make sure it only increases and never decreases.

    Second time source: GPS

    See Sync Android devices via GPS time?

    Third time source: Cell network

    AFAIK at least some cell networks have an internal time. Not sure how and if you can access that on android.

    Fourth time source: Internet

    If you detect an active Internet connection get the time from a trusted time source.