@@ -132,46 +132,38 @@ public void onReceive(Context context, Intent intent) {
132132 Log .d (TAG , "Ping " + count + " times." );
133133
134134 Log .d (TAG , "Check time :" + System .currentTimeMillis ());
135- IMqttToken token = comms .checkForActivity ();
136135
137- // No ping has been sent.
138- if ( token == null ) {
139- return ;
140- }
136+ PowerManager pm = ( PowerManager ) service
137+ . getSystemService ( Service . POWER_SERVICE );
138+ wakelock = pm . newWakeLock ( PowerManager . PARTIAL_WAKE_LOCK , wakeLockTag ) ;
139+ wakelock . acquire ();
141140
142141 // Assign new callback to token to execute code after PingResq
143142 // arrives. Get another wakelock even receiver already has one,
144143 // release it until ping response returns.
145- if (wakelock == null ) {
146- PowerManager pm = (PowerManager ) service
147- .getSystemService (Service .POWER_SERVICE );
148- wakelock = pm .newWakeLock (PowerManager .PARTIAL_WAKE_LOCK ,
149- wakeLockTag );
150- }
151- wakelock .acquire ();
152- token .setActionCallback (new IMqttActionListener () {
144+ IMqttToken token = comms .checkForActivity (new IMqttActionListener () {
153145
154146 @ Override
155147 public void onSuccess (IMqttToken asyncActionToken ) {
156148 Log .d (TAG , "Success. Release lock(" + wakeLockTag + "):"
157149 + System .currentTimeMillis ());
158150 //Release wakelock when it is done.
159- if (wakelock != null && wakelock .isHeld ()){
160- wakelock .release ();
161- }
151+ wakelock .release ();
162152 }
163153
164154 @ Override
165155 public void onFailure (IMqttToken asyncActionToken ,
166- Throwable exception ) {
156+ Throwable exception ) {
167157 Log .d (TAG , "Failure. Release lock(" + wakeLockTag + "):"
168158 + System .currentTimeMillis ());
169159 //Release wakelock when it is done.
170- if (wakelock != null && wakelock .isHeld ()){
171- wakelock .release ();
172- }
160+ wakelock .release ();
173161 }
174162 });
163+
164+ if (token == null ) {
165+ wakelock .release ();
166+ }
175167 }
176168 }
177169}
0 commit comments