Boarded departure service now restarts with intent info (so the notification doesn't randomly "freeze")

This commit is contained in:
Doug Keen 2013-03-24 16:12:17 -07:00
parent 601d8516e6
commit 0c51fe904d
3 changed files with 28 additions and 9 deletions

View File

@ -19,6 +19,7 @@ import android.util.Log;
import com.dougkeen.bart.BartRunnerApplication; import com.dougkeen.bart.BartRunnerApplication;
import com.dougkeen.bart.R; import com.dougkeen.bart.R;
import com.dougkeen.bart.activities.ViewDeparturesActivity;
import com.dougkeen.bart.services.BoardedDepartureService; import com.dougkeen.bart.services.BoardedDepartureService;
import com.dougkeen.util.Observable; import com.dougkeen.util.Observable;
@ -533,8 +534,10 @@ public class Departure implements Parcelable, Comparable<Departure> {
} }
private PendingIntent getAlarmIntent(Context context) { private PendingIntent getAlarmIntent(Context context) {
return PendingIntent.getBroadcast(context, 0, new Intent( Intent intent = new Intent(context, ViewDeparturesActivity.class);
Constants.ACTION_ALARM, getStationPair().getUri()), intent.putExtra(Constants.STATION_PAIR_EXTRA, getStationPair());
intent.setAction(Constants.ACTION_ALARM);
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
} }
@ -579,8 +582,10 @@ public class Departure implements Parcelable, Comparable<Departure> {
private PendingIntent getNotificationIntent(Context context) { private PendingIntent getNotificationIntent(Context context) {
if (notificationIntent == null) { if (notificationIntent == null) {
Intent targetIntent = new Intent(Intent.ACTION_VIEW, Intent targetIntent = new Intent(context,
getStationPair().getUri()); ViewDeparturesActivity.class);
targetIntent.putExtra(Constants.STATION_PAIR_EXTRA,
getStationPair());
targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent = PendingIntent.getActivity(context, 0, notificationIntent = PendingIntent.getActivity(context, 0,
targetIntent, PendingIntent.FLAG_UPDATE_CURRENT); targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);

View File

@ -5,6 +5,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import com.dougkeen.bart.BartRunnerApplication; import com.dougkeen.bart.BartRunnerApplication;
import com.dougkeen.bart.activities.ViewDeparturesActivity;
import com.dougkeen.bart.model.Constants;
import com.dougkeen.bart.model.Departure; import com.dougkeen.bart.model.Departure;
import com.dougkeen.util.WakeLocker; import com.dougkeen.util.WakeLocker;
@ -24,8 +26,9 @@ public class AlarmBroadcastReceiver extends BroadcastReceiver {
application.setPlayAlarmRingtone(true); application.setPlayAlarmRingtone(true);
Intent targetIntent = new Intent(Intent.ACTION_VIEW, boardedDeparture Intent targetIntent = new Intent(context, ViewDeparturesActivity.class);
.getStationPair().getUri()); targetIntent.putExtra(Constants.STATION_PAIR_EXTRA,
boardedDeparture.getStationPair());
targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(targetIntent); context.startActivity(targetIntent);

View File

@ -109,7 +109,7 @@ public class BoardedDepartureService extends Service implements
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
mHasShutDown = false; mHasShutDown = false;
onStart(intent, startId); onStart(intent, startId);
return START_STICKY; return START_REDELIVER_INTENT;
} }
@Override @Override
@ -122,10 +122,21 @@ public class BoardedDepartureService extends Service implements
} }
protected void onHandleIntent(Intent intent) { protected void onHandleIntent(Intent intent) {
if (intent == null)
return;
final BartRunnerApplication application = (BartRunnerApplication) getApplication(); final BartRunnerApplication application = (BartRunnerApplication) getApplication();
final Departure boardedDeparture = application.getBoardedDeparture(); final Departure boardedDeparture;
if (boardedDeparture == null || intent == null) { if (intent.hasExtra("departure")) {
boardedDeparture = intent.getExtras().getParcelable("departure");
} else {
boardedDeparture = application.getBoardedDeparture();
}
if (boardedDeparture == null) {
// Nothing to notify about // Nothing to notify about
if (mNotificationManager != null) {
mNotificationManager.cancel(DEPARTURE_NOTIFICATION_ID);
}
return; return;
} }
if (intent.getBooleanExtra("cancelNotifications", false) if (intent.getBooleanExtra("cancelNotifications", false)