About the lock screen status of the androidpn message push client, the heartbeat packet cannot be sent


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();
	}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325601829&siteId=291194637