Class ServerPingWithAlarmManager
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 Summary
Modifier and TypeMethodDescriptionstatic ServerPingWithAlarmManager
getInstanceFor
(XMPPConnection connection) Get the instance of this manager for the given connection.boolean
Check if this manager is enabled.static void
onCreate
(android.content.Context context) Register a pending intent with the AlarmManager to be broadcast every half hour and register the alarm broadcast receiver to receive this intent.static void
Unregister the alarm broadcast receiver and cancel the alarm.void
setEnabled
(boolean enabled) If enabled, ServerPingWithAlarmManager will callPingManager.pingServerIfNecessary()
for the connection of this instance every half hour.Methods inherited from class org.jivesoftware.smack.Manager
connection, getAuthenticatedConnectionOrThrow, schedule, schedule, scheduleBlocking
-
Method Details
-
getInstanceFor
Get the instance of this manager for the given connection.- Parameters:
connection
- the connection.- Returns:
- the instance of this manager for the given connection.
-
setEnabled
If enabled, ServerPingWithAlarmManager will callPingManager.pingServerIfNecessary()
for the connection of this instance every half hour.- Parameters:
enabled
- whether or not this manager is should be enabled or not.
-
isEnabled
Check if this manager is enabled.- Returns:
true
if this manager is enabled,false
otherwise.
-
onCreate
Register a pending intent with the AlarmManager to be broadcast 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
Unregister the alarm broadcast receiver and cancel the alarm.
-