Skip to content

Commit 7d20684

Browse files
author
Devendra
committed
adding partial wake lock options to demo console and subscribe at boot app
1 parent bb37031 commit 7d20684

6 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎android/examples/PubnubExample/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
<uses-permission android:name="android.permission.INTERNET"/>
2525
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2626
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
27+
<uses-permission android:name="android.permission.WAKE_LOCK"/>
2728

2829
</manifest>

‎android/examples/PubnubExample/res/menu/activity_main.xml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414
<item android:id="@+id/option13" android:title="@string/option13" android:titleCondensed="@string/option13"></item>
1515
<item android:id="@+id/option14" android:title="@string/option14" android:titleCondensed="@string/option14"></item>
1616
<item android:id="@+id/option15" android:title="@string/option15" android:titleCondensed="@string/option15"></item>
17-
17+
<item android:id="@+id/option16" android:title="@string/option16" android:titleCondensed="@string/option16"></item>
18+
<item android:id="@+id/option17" android:title="@string/option17" android:titleCondensed="@string/option17"></item>
19+
<item android:id="@+id/option18" android:title="@string/option18" android:titleCondensed="@string/option18"></item>
1820
</menu>

‎android/examples/PubnubExample/res/values/strings.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
<string name="option13">Set Retry Interval</string>
3535
<string name="option14">Set Subscribe Timeout</string>
3636
<string name="option15">Set Non Subscribe Timeout</string>
37+
<string name="option16">Acquire Partial Wake Lock</string>
38+
<string name="option17">Release Partial Wake Lock</string>
39+
<string name="option18">Check Wake Lock Status</string>
3740

3841

3942
</resources>

‎android/examples/PubnubExample/src/com/pubnub/examples/pubnubExample/MainActivity.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.content.IntentFilter;
1515
import android.net.ConnectivityManager;
1616
import android.os.Bundle;
17+
import android.os.PowerManager;
1718
import android.text.InputType;
1819
import android.util.Log;
1920
import android.view.Menu;
@@ -34,6 +35,7 @@ public class MainActivity extends Activity {
3435
*/
3536

3637
Pubnub pubnub = new Pubnub("demo", "demo", "", false);
38+
PowerManager.WakeLock wl = null;
3739

3840
private void notifyUser(Object message) {
3941
try {
@@ -88,6 +90,8 @@ public void onReceive(Context arg0, Intent intent) {
8890
}
8991

9092
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
93+
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
94+
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PubnubDemoConsole");
9195

9296
}
9397

@@ -163,11 +167,37 @@ public boolean onOptionsItemSelected(MenuItem item) {
163167
case R.id.option15:
164168
setNonSubscribeTimeout();
165169
return true;
170+
171+
case R.id.option16:
172+
acquirePartialWakeLock();
173+
return true;
174+
175+
case R.id.option17:
176+
releasePartialWakeLock();
177+
return true;
178+
179+
case R.id.option18:
180+
checkWakeLockStatus();
181+
return true;
166182
default:
167183
return super.onOptionsItemSelected(item);
168184
}
169185
}
170186

187+
private void checkWakeLockStatus() {
188+
notifyUser("Partial Wake Lock : " + wl.isHeld());
189+
}
190+
191+
private void releasePartialWakeLock() {
192+
if (wl != null) wl.release();
193+
notifyUser("Partial Wake Lock : " + wl.isHeld());
194+
}
195+
196+
private void acquirePartialWakeLock() {
197+
if (wl != null) wl.acquire();
198+
notifyUser("Partial Wake Lock : " + wl.isHeld());
199+
}
200+
171201
private void setNonSubscribeTimeout() {
172202

173203
AlertDialog.Builder builder = new AlertDialog.Builder(this);

‎android/examples/SubscribeAtBoot/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535

3636
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
3737
<uses-permission android:name="android.permission.INTERNET"/>
38+
<uses-permission android:name="android.permission.WAKE_LOCK"/>
3839

3940
</manifest>

‎android/examples/SubscribeAtBoot/src/com/pubnub/examples/subscribeAtBoot/PubnubService.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import android.os.Handler;
44
import android.os.Message;
5+
import android.os.PowerManager;
56

67
import com.pubnub.api.Callback;
78
import com.pubnub.api.Pubnub;
89
import com.pubnub.api.PubnubException;
910

1011

1112
import android.app.Service;
13+
import android.content.Context;
1214
import android.content.Intent;
1315
import android.os.IBinder;
1416
import 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

Comments
 (0)