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



我是Android工作的新手,我不确定我是否把自己写进了一个角落,或者我只是没有看到解决方案(两者都有可能)。我有一个意图服务,它根据用户保存的时间设置重复警报。当警报响起时,目的是显示一条通知,如果用户点击,将发送地图应用的隐含意图,并使用用户保存的地址填写起点和终点,以便他们可以运行搜索并检查他们的通勤时间。

我的问题是,我不确定如何编写 onHandleIntent 方法,以便根据发送的警报将具有正确起点和终点的隐含地图意图打包在附加内容中。它是否与待处理意图中设置的请求代码有关,或者它是否像在自己的 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();
}

您可以简单地通过包装在待定意图中的 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)
...

在您的主要活动中,只需阅读我们刚刚放置的那些附加内容。

相关内容

最新更新