Android:如何从NotificationManager操作的PendingIntent中调用IntentService

4
我有一个推送通知应用程序。 当推送通知到达时,广播接收器会调用GCMIntentService来设置通知。
mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, HomeActivity.class);
        Intent intent = new Intent(this, AddToCalendarIntentService.class);
        intent.putExtra(JSON_KEY_CLASS_ID, classid);
        intent.putExtra(JSON_KEY_CLASS_NAME, classname);
        intent.putExtra(Config.JSON_KEY_DATE, tgl);
        intent.putExtra(Config.JSON_KEY_TIME_START, timestart);
        intent.putExtra(Config.JSON_KEY_TIME_END, timeend);
        intent.putExtra(Config.JSON_KEY_VENUE, venue);
        Log.d(LOG_TAG, classid + ", " + classname + ", " + venue);
        PendingIntent contentIntent = PendingIntent.getService(this, 0,
                intent, 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_notif)
                        .setContentTitle("Re: " + classname)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(pesan))
                        .setContentText(pesan)
                        .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                        .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
                        .setDefaults(NotificationCompat.DEFAULT_SOUND)
                        .addAction(R.drawable.ic_action_add_person, "Add to Calendar", contentIntent)
                        .setAutoCancel(true)
                        .setTicker("Reschedule Class");

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

当用户点击“添加到日历”时,它会调用PendingIntent来启动AddToCalendarIntentService并传递所有参数。
public class AddToCalendarIntentService extends IntentService {

public AddToCalendarIntentService() {
    super("AddToCalendarIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    final String JSON_KEY_CLASS_NAME = "classname";
    final String JSON_KEY_CLASS_ID = "classid";
    Bundle extra = intent.getExtras();
    String title = extra.getString(JSON_KEY_CLASS_NAME);
    String location = extra.getString(Config.JSON_KEY_VENUE);
    String tgl = extra.getString(Config.JSON_KEY_DATE);
    String[] tglSplit = tgl.split("-");
    int year = Integer.parseInt(tglSplit[0]);
    int month = Integer.parseInt(tglSplit[1]);
    int date = Integer.parseInt(tglSplit[2]);

    String timestart = extra.getString(Config.JSON_KEY_TIME_START);
    String timeend = extra.getString(Config.JSON_KEY_TIME_END);
    String[] start = timestart.split(":");
    String[] end = timeend.split(":");
    int hourStart = Integer.parseInt(start[0]);
    int minStart = Integer.parseInt(start[1]);
    int hourEnd = Integer.parseInt(end[0]);
    int minEnd = Integer.parseInt(end[1]);
    Log.d("INTENT SERVICE", location);
    TaskHelper.addToCalendar(this, "Reschedule: " + title, location, year, month-1, date, hourStart, minStart, hourEnd, minEnd);
    NotificationManager mNotification = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotification.cancel(1);
}
}

代码可以正常运行,但如果我再次收到另一个通知,并将其添加到日历中,则AddToCalendarIntentService中的参数将保留旧参数,忽略新参数。例如,在第一次通知中,位置为Indonesia,GCMIntentService和AddToCalendarIntentService的日志都显示Indonesia。
在第二个通知中,位置为Singapore,在GCMIntentService中正确显示,但在AddToCalendarIntentService中记录时显示Indonesia(应该是Singapore)。
请帮忙解决问题。
谢谢。

阅读有关 getService() 函数第二个参数的文档。 - pskink
2个回答

3

系统之所以认为这是相同的PendingIntent,是因为它们的请求码(PendingIntent.getService方法的第二个参数)相同,如果你想要多个不同的PendingIntent,就需要对它们进行区分。

最简单的方法是提供不同的请求码。

你可以在这里阅读更多信息:

http://developer.android.com/reference/android/app/PendingIntent.html


1
正如TomerZ所指出的那样,系统认为这是相同的PendingIntent。在您的待处理意图中添加Flag: PendingIntent.FLAG_UPDATE_CURRENT以通知系统更新待处理意图的extras。
PendingIntent contentIntent = PendingIntent.getService(this, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

FLAG_UPDATE_CURRENT 的文档供参考


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接