public final class ServerPingWithAlarmManager extends Manager
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
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.
Modifier and Type | Method and 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 call
PingManager.pingServerIfNecessary()
for the connection of this instance every half hour. |
connection, getAuthenticatedConnectionOrThrow, schedule
public static ServerPingWithAlarmManager getInstanceFor(XMPPConnection connection)
public void setEnabled(boolean enabled)
PingManager.pingServerIfNecessary()
for the connection of this instance every half hour.enabled
- public boolean isEnabled()
public static void onCreate(android.content.Context context)
context
- public static void onDestroy()