我刚开始在Android工作,我不确定自己是否写到角落里,或者我是否只是没有看到解决方案(可能是这样)。我有一个intentservice,它根据用户保存的时间设置重复警报。当警报响起时,目的是显示一个通知,如果用户点击将发送地图应用程序的隐含意图,并用用户保存的地址填写起点和终点,这样他们就可以运行搜索和检查他们的通勤时间。
我的问题是,我不确定如何编写onHandleIntent方法,以便根据正在发送的警报,在附加内容中包含正确的Start和End点的隐含地图意图。它是否与挂起意图中设置的请求代码有关,还是像在自己的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();
}
答案 0 :(得分:1)
您可以通过您在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中,只需阅读我们刚才提供的附加内容。