1. The client is in locked screen state and cannot send heartbeat packets
Solution: Use a power lock
Client: NotificationService
Add attributes:
/** * Device power lock. */ private PowerManager.WakeLock mWakeLock;
/** * Apply for device power lock */ private final void acquireWakeLock() { if (mWakeLock == null) { PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getPackageName()); } if (mWakeLock != null) { mWakeLock.acquire(); Log.d(LOGTAG, "mWakeLock.acquire()"); } } /** * Release the device power lock */ private final void releaseWakeLock() { Log.d(LOGTAG, "releaseWakeLock"); if (mWakeLock != null) { mWakeLock.release(); mWakeLock = null; } }
@Override public void onStart(Intent intent, int startId) { Log.d(LOGTAG, "onStart()..."); acquireWakeLock(); }
private void stop() { Log.d(LOGTAG, "stop()..."); unregisterNotificationReceiver(); unregisterConnectivityReceiver(); xmppManager.disconnect(); executorService.shutdown(); releaseWakeLock(); }