Class ServerPingWithAlarmManager


  • public final class ServerPingWithAlarmManager
    extends Manager
    Send automatic server pings with the help of AlarmManager.

    Smack's PingManager uses a ScheduledThreadPoolExecutor to schedule the automatic server pings, but on Android, those scheduled pings are not reliable. This is because the Android device may go into deep sleep where the system will not continue to run this causes

    • the system time to not move forward, which means that the time spent in deep sleep is not counted towards the scheduled delay time
    • the scheduled Runnable is not run while the system is in deep sleep.

    That is the reason Android comes with an API to schedule those tasks: AlarmManager. Which this class uses to determine every 30 minutes if a server ping is necessary. The interval of 30 minutes is the ideal trade-off between reliability and low resource (battery) consumption.

    In order to use this class you need to call onCreate(Context) once, for example in the onCreate() method of your Service holding the XMPPConnection. And to avoid leaking any resources, you should call onDestroy() when you no longer need any of its functionality.

    • Method Detail

      • setEnabled

        public void setEnabled​(boolean enabled)
        If enabled, ServerPingWithAlarmManager will call PingManager.pingServerIfNecessary() for the connection of this instance every half hour.
        Parameters:
        enabled - whether or not this manager is should be enabled or not.
      • onCreate

        public static void onCreate​(android.content.Context context)
        Register a pending intent with the AlarmManager to be broadcasted every half hour and register the alarm broadcast receiver to receive this intent. The receiver will check all known questions if a ping is Necessary when invoked by the alarm intent.
        Parameters:
        context - an Android context.
      • onDestroy

        public static void onDestroy()
        Unregister the alarm broadcast receiver and cancel the alarm.