0

I have an android app where I am trying to play videos based on the server's clock time(not the android device's clock time which might be different)

08:00:00 - 08:04:59 -> video_url_1

08:05:00 - 08:09:59 -> video_url_2

08:10:00 - 08:14:59 -> video_url_3

So when the user opens the app,if the server's clock time falls in the above category,then the respective videos are played,otherwise a timer for remaining seconds is shown based on the server's clock time.

If the android app is already open,then it should automatically start playing video when the server's clock time falls in any of the above categories.

Continuously pinging the server for its clock time is not a good option.So how can we achieve the above functionality?

Ayan
  • 8,192
  • 4
  • 46
  • 51
  • 1
    1) Using push notifications (send push from server when you need to change something) 2) store difference between server and local time, and fire events based on local time + difference 3) ping server periodically. – Vladyslav Matviienko Mar 21 '18 at 10:45
  • Hi @VladyslavMatviienko ,the android user might manually change his phone's clock time after I have stored the difference between server and clock time.In that case,option 2 would not work properly.Also there are millions of users.So option 3 would actually cause a huge unnecessary server load. Any idea on how to solve such a case? – Ayan Mar 21 '18 at 11:13
  • 1
    as for user changing time - you may stick to the system uptime instead of device time to calculate server time. This can't be changed by the user, but will reset on reboot. If you expect millions of users, you should be able to afford powerful enough servers to handle such load. Also I got 1 more idea: you can just get time from the internet time servers. This way it won't overload your own server: https://stackoverflow.com/questions/13064750/how-to-get-current-time-from-internet-in-android – Vladyslav Matviienko Mar 21 '18 at 11:30

1 Answers1

2

I think Alarm Manager is your solution.

The flow would be the following:

  1. Android app opens and request the server what the server time is and when is the next video.
  2. Calculate how in how many minute or seconds the time will be done.
  3. Setup an alarm in the device for that time. Let's say the server time is 8:45 and the next video is at 9:30. So you know 45 minutes has to pass in order to be able to play the video. So the alarm should be trigger in 45 minutes.
  4. When the alarm is trigger, check the server again and if it's fine, play the video and schedule your next alarm (for the next video or if the server now wants to play the video in another moment), if the user fake his time, then recalculate the time that need to pass and reschedule your alarm.

Set the alarm manager example to trigger a broadcast receiver:

AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(context, MyBroadCastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
// add here your calculate minutes or seconds or hours
calendar.add(Calendar.MINUTE, 45);
if (alarmManager != null) {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

Then have your broadcast receiver to handle any other action:

public class MyBroadcastReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        // Do your magic, could be play the video or show a notification saying the video is ready to play
    }
}

If you want more information about the alarms in Android, take a look at this link:

https://developer.android.com/training/scheduling/alarms.html

Javier Vieira
  • 2,100
  • 20
  • 23