Harry Tuttle
Harry Tuttle

Reputation: 27

Different implied intents in onHandleIntent between two notifications

I'm new to working in Android, and I'm not sure if I wrote myself into a corner or if I'm just not seeing the solution (either is likely). I have an intentservice which sets repeating alarms based on those times a user saves. When the alarms go off, the intention is to display a notification that if the user taps on will send off an implied intent for a maps app and fills out the Start and End points with addresses the user saves, so they can run a search and check their commute times.

My question is that I'm unsure how to write the onHandleIntent method so that the implied map intent with the correct Start and End points packaged in the extras, based on which alarm is being sent. Does it have to do with the request codes set in pending intents, or is it as simple as creating two notifications in their own if blocks, checking for intent extras? Again, I'm unsure and would appreciate some help. I currently have one notification that I wrote, but that was just a dummy for testing to make sure the alarms are firing (they are). Pasted below are the two relevant methods:

@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "MapQueryService is handing intent: " + intent);
Resources r=getResources();
PendingIntent notificationIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

 Notification notification = new NotificationCompat.Builder(this)
.setTicker("Ticker Here!")
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentTitle("Content Title Here!")
.setContentText("Content Text Here!")
.setContentIntent(notificationIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(3, notification);
    }

//this method is for either the main menu (via toggle button) or broadcast receiver to call to turn the intentservice on/off.
public static void setServiceAlarm(Context context, boolean isOn, UserData userInfo)
{

Intent workCommuteIntent = new Intent(context, CommuteCheckAlarmService.class);
PendingIntent pendingWorkCommuteIntent = PendingIntent.getService(context, 0, workCommuteIntent, 0);
Intent homeCommuteIntent = new Intent(context, CommuteCheckAlarmService.class);
PendingIntent pendingHomeCommuteIntent = PendingIntent.getService(context, 0, homeCommuteIntent, 0);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

long workTime = userInfo.getDriveToWorkTime().getTimeInMillis();
long homeTime = userInfo.getDriveToHomeTime().getTimeInMillis();

if (isOn)
{

GregorianCalendar today = (GregorianCalendar) Calendar.getInstance();
Log.i(ALARM_SERVICE, "today.DAY_OF_WEEK is " + today.get(Calendar.DAY_OF_WEEK));
Log.i(ALARM_SERVICE, "getworkweek[0] is " + userInfo.getWorkWeek().toString());

for (Day day : userInfo.getWorkWeek())
if (day.get()==today.get(Calendar.DAY_OF_WEEK))

{
alarmManager.setRepeating(AlarmManager.RTC, workTime, AlarmManager.INTERVAL_DAY, pendingWorkCommuteIntent);
alarmManager.setRepeating(AlarmManager.RTC, homeTime, AlarmManager.INTERVAL_DAY, pendingHomeCommuteIntent);
Log.i(ALARM_SERVICE, "ServiceAlarm active!");
Log.i(ALARM_SERVICE, "worktime is " + workTime);
Log.i(ALARM_SERVICE, "hometime is " + homeTime);
}
}

else
{
alarmManager.cancel(pendingWorkCommuteIntent);
pendingWorkCommuteIntent.cancel();
alarmManager.cancel(pendingHomeCommuteIntent);
pendingHomeCommuteIntent.cancel();
Log.i(ALARM_SERVICE, "ServiceAlarm currently off!");
}

PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean(PREF_IS_ALARM_ON, isOn)
.commit();
}

Upvotes: 0

Views: 167

Answers (1)

Vesko
Vesko

Reputation: 3760

You can simply send those data as extras via the Intent you're wrapping in a PendingIntent. So in your onHandleIntent() method:

...
Intent mapIntent = new Intent(this, MainActivity.class);
mapIntent.putExtra("Start", xxxx);
mapIntent.putExtra("Start", xxxx);
PendingIntent notificationIntent = PendingIntent.getActivity(this, 0, mapIntent, 0);

Notification notification = new NotificationCompat.Builder(this)
...

And in your MainActivity simple read those extras we just put.

Upvotes: 1

Related Questions