Class ServerPingWithAlarmManager
- java.lang.Object
-
- org.jivesoftware.smack.Manager
-
- org.jivesoftware.smackx.ping.android.ServerPingWithAlarmManager
-
public final class ServerPingWithAlarmManager extends Manager
Send automatic server pings with the help ofAlarmManager
.Smack's
PingManager
uses aScheduledThreadPoolExecutor
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 theonCreate()
method of your Service holding the XMPPConnection. And to avoid leaking any resources, you should callonDestroy()
when you no longer need any of its functionality.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ServerPingWithAlarmManager
getInstanceFor(XMPPConnection connection)
boolean
isEnabled()
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.static void
onDestroy()
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 Detail
-
getInstanceFor
public static ServerPingWithAlarmManager getInstanceFor(XMPPConnection connection)
-
setEnabled
public void setEnabled(boolean enabled)
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
public boolean isEnabled()
-
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.
-
-