|
1 | 1 | package com.pubnub.examples.pubnubsubscribe; |
2 | 2 |
|
| 3 | +import java.lang.Thread.UncaughtExceptionHandler; |
| 4 | + |
3 | 5 | import org.json.JSONArray; |
4 | 6 | import org.json.JSONException; |
5 | 7 | import org.json.JSONObject; |
|
17 | 19 | import com.pubnub.api.PubnubError; |
18 | 20 | import com.pubnub.api.PubnubException; |
19 | 21 |
|
| 22 | +import android.app.AlarmManager; |
20 | 23 | import android.app.Notification; |
21 | 24 | import android.app.NotificationManager; |
22 | 25 | import android.app.PendingIntent; |
23 | 26 | import android.app.Service; |
24 | 27 | import android.content.Context; |
25 | 28 | import android.content.Intent; |
| 29 | +import android.content.IntentFilter; |
26 | 30 | import android.graphics.Color; |
27 | 31 | import android.os.IBinder; |
| 32 | +import android.provider.SyncStateContract.Constants; |
28 | 33 | import android.support.v4.app.NotificationCompat; |
29 | 34 | import android.util.Log; |
30 | 35 | import android.widget.Toast; |
@@ -126,24 +131,15 @@ private void notifyUser(JSONObject message) { |
126 | 131 | System.out.println(s.toString()); |
127 | 132 | } |
128 | 133 | } |
129 | | - |
| 134 | + /* |
130 | 135 | public int onStartCommand(Intent intent, int flags, int startId) { |
131 | 136 | super.onStartCommand(intent, flags, startId); |
132 | 137 | return START_STICKY; |
133 | 138 | } |
134 | | - |
135 | | - public void onCreate() { |
136 | | - super.onCreate(); |
137 | | - initPubnub(); |
138 | | - PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
139 | | - wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SubscribeAtBoot"); |
140 | | - if (wl != null) { |
141 | | - wl.acquire(); |
142 | | - Log.i("PUBNUB", "Partial Wake Lock : " + wl.isHeld()); |
143 | | - } |
144 | | - |
145 | | - Log.i("PUBNUB", "PubnubService created..."); |
146 | | - |
| 139 | + */ |
| 140 | + |
| 141 | + public void listen() { |
| 142 | + |
147 | 143 | try { |
148 | 144 | pubnub.subscribe(public_channel, new Callback() { |
149 | 145 | @Override |
@@ -180,7 +176,41 @@ public void errorCallback(String channel, Object message) { |
180 | 176 | } catch (PubnubException e) { |
181 | 177 | Log.i("PUBNUB", e.toString()); |
182 | 178 | } |
| 179 | + } |
183 | 180 |
|
| 181 | + public void onCreate() { |
| 182 | + super.onCreate(); |
| 183 | + initPubnub(); |
| 184 | + PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 185 | + wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SubscribeAtBoot"); |
| 186 | + if (wl != null) { |
| 187 | + wl.acquire(); |
| 188 | + Log.i("PUBNUB", "Partial Wake Lock : " + wl.isHeld()); |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | + registerReceiver(new BootReceiver(), |
| 193 | + new IntentFilter("com.pubnub.examples.pubnubsubscribe.USER_ACTION")); |
| 194 | + |
| 195 | + Intent intent = new Intent(this, PubnubActivity.class); |
| 196 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 197 | + this.startActivity(intent); |
| 198 | + |
| 199 | + |
| 200 | + final UncaughtExceptionHandler defaultUEH = Thread.getDefaultUncaughtExceptionHandler();; |
| 201 | + Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { |
| 202 | + @Override |
| 203 | + public void uncaughtException(Thread paramThread, Throwable paramThrowable) { |
| 204 | + Log.i("PUBNUB","Global Exception Service"); |
| 205 | + Thread.setDefaultUncaughtExceptionHandler(defaultUEH); |
| 206 | + defaultUEH.uncaughtException(paramThread, paramThrowable); |
| 207 | + Intent i = new Intent("com.pubnub.examples.pubnubsubscribe.USER_ACTION"); |
| 208 | + sendBroadcast(i); |
| 209 | + } |
| 210 | + }); |
| 211 | + |
| 212 | + Log.i("PUBNUB", "PubnubService created..."); |
| 213 | + listen(); |
184 | 214 | } |
185 | 215 |
|
186 | 216 | @Override |
|
0 commit comments