两个通知之间 onHandleIntent 中的不同隐含意图

Different implied intents in onHandleIntent between two notifications

我刚开始在 Android 工作,我不确定我是把自己逼到了墙角,还是只是没有看到解决方案(两种可能性都有)。我有一个 intentservice,它根据用户保存的时间设置重复警报。当警报响起时,目的是显示一条通知,如果用户点击将发送地图应用程序的隐含意图,并使用用户保存的地址填写起点和终点,这样他们就可以 运行 搜索并查看他们的通勤时间。

我的问题是,我不确定如何编写 onHandleIntent 方法,以便隐含的地图意图与正确的开始和结束点打包在 extras 中,基于发送的警报。它是否与在未决意图中设置的请求代码有关,或者它是否像在它们自己的 if 块中创建两个通知一样简单,检查意图额外?同样,我不确定并希望得到一些帮助。我目前有一个我写的通知,但那只是一个用于测试的假人,以确保警报正在触发(它们是)。下面粘贴两个相关方法:

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

您只需通过包装在 PendingIntent 中的 Intent 将这些数据作为额外发送。所以在你的 onHandleIntent() 方法中:

...
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)
...

然后在您的 MainActivity 中简单阅读我们刚刚添加的那些额外内容。