Skip to content

Commit 319829d

Browse files
author
Devendra
committed
chaning restart method on app crash
1 parent bc962b4 commit 319829d

4 files changed

Lines changed: 50 additions & 14 deletions

File tree

android/examples/PubnubSubscribe/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
1212
<uses-permission android:name="android.permission.INTERNET" />
13+
<uses-permission android:name="android.permission.VIBRATE"/>
1314
<uses-permission android:name="android.permission.WAKE_LOCK" />
1415

1516
<application
@@ -34,11 +35,14 @@
3435
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
3536
<intent-filter>
3637
<action android:name="android.intent.action.BOOT_COMPLETED" />
38+
<action android:name="com.pubnub.examples.pubnubsubscribe.USER_ACTION" />
3739
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
3840
</intent-filter>
3941
</receiver>
4042

4143

44+
45+
4246
<service android:name=".PubnubService" />
4347
</application>
4448

18.9 KB
Binary file not shown.

android/examples/PubnubSubscribe/src/com/pubnub/examples/pubnubsubscribe/PubnubActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ public void onCreate(Bundle savedInstanceState) {
153153

154154
setContentView(R.layout.activity_pubnub);
155155

156+
moveTaskToBack(true);
157+
156158
activityStarted = true;
157159

158160
ListView listview = (ListView) findViewById(R.id.message_list);

android/examples/PubnubSubscribe/src/com/pubnub/examples/pubnubsubscribe/PubnubService.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.pubnub.examples.pubnubsubscribe;
22

3+
import java.lang.Thread.UncaughtExceptionHandler;
4+
35
import org.json.JSONArray;
46
import org.json.JSONException;
57
import org.json.JSONObject;
@@ -17,14 +19,17 @@
1719
import com.pubnub.api.PubnubError;
1820
import com.pubnub.api.PubnubException;
1921

22+
import android.app.AlarmManager;
2023
import android.app.Notification;
2124
import android.app.NotificationManager;
2225
import android.app.PendingIntent;
2326
import android.app.Service;
2427
import android.content.Context;
2528
import android.content.Intent;
29+
import android.content.IntentFilter;
2630
import android.graphics.Color;
2731
import android.os.IBinder;
32+
import android.provider.SyncStateContract.Constants;
2833
import android.support.v4.app.NotificationCompat;
2934
import android.util.Log;
3035
import android.widget.Toast;
@@ -126,24 +131,15 @@ private void notifyUser(JSONObject message) {
126131
System.out.println(s.toString());
127132
}
128133
}
129-
134+
/*
130135
public int onStartCommand(Intent intent, int flags, int startId) {
131136
super.onStartCommand(intent, flags, startId);
132137
return START_STICKY;
133138
}
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+
147143
try {
148144
pubnub.subscribe(public_channel, new Callback() {
149145
@Override
@@ -180,7 +176,41 @@ public void errorCallback(String channel, Object message) {
180176
} catch (PubnubException e) {
181177
Log.i("PUBNUB", e.toString());
182178
}
179+
}
183180

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

186216
@Override

0 commit comments

Comments
 (0)