22
33import android .os .Handler ;
44import android .os .Message ;
5+ import android .os .PowerManager ;
56
67import com .pubnub .api .Callback ;
78import com .pubnub .api .Pubnub ;
89import com .pubnub .api .PubnubException ;
910
1011
1112import android .app .Service ;
13+ import android .content .Context ;
1214import android .content .Intent ;
1315import android .os .IBinder ;
1416import android .util .Log ;
@@ -18,6 +20,7 @@ public class PubnubService extends Service {
1820
1921 String channel = "hello_world" ;
2022 Pubnub pubnub = new Pubnub ("demo" , "demo" , false );
23+ PowerManager .WakeLock wl = null ;
2124
2225 private final Handler handler = new Handler () {
2326 public void handleMessage (Message msg ) {
@@ -55,6 +58,14 @@ private void notifyUser(Object message) {
5558 public void onCreate () {
5659 super .onCreate ();
5760 Toast .makeText (this , "PubnubService created..." , Toast .LENGTH_LONG ).show ();
61+ PowerManager pm = (PowerManager ) getSystemService (Context .POWER_SERVICE );
62+ wl = pm .newWakeLock (PowerManager .PARTIAL_WAKE_LOCK , "SubscribeAtBoot" );
63+ if (wl != null ) {
64+ wl .acquire ();
65+ Log .i ("PUBNUB" , "Partial Wake Lock : " + wl .isHeld ());
66+ Toast .makeText (this , "Partial Wake Lock : " + wl .isHeld (), Toast .LENGTH_LONG ).show ();
67+ }
68+
5869 Log .i ("PUBNUB" , "PubnubService created..." );
5970 try {
6071 pubnub .subscribe (new String [] {channel }, new Callback () {
@@ -84,6 +95,12 @@ public void errorCallback(String channel, Object message) {
8495 @ Override
8596 public void onDestroy () {
8697 super .onDestroy ();
98+ if (wl != null ) {
99+ wl .release ();
100+ Log .i ("PUBNUB" , "Partial Wake Lock : " + wl .isHeld ());
101+ Toast .makeText (this , "Partial Wake Lock : " + wl .isHeld (), Toast .LENGTH_LONG ).show ();
102+ wl = null ;
103+ }
87104 Toast .makeText (this , "PubnubService destroyed..." , Toast .LENGTH_LONG ).show ();
88105 }
89106
0 commit comments