gpt4 book ai didi

android - 所需的 ListView 的项目未被选中

转载 作者:行者123 更新时间:2023-11-30 01:19:18 25 4
gpt4 key购买 nike

这段代码覆盖 Activity的onNewIntent()方法。在此方法中,我将 selected ListView 的一项设置为单击通知的结果。问题是;当有两个通知时,单击第一个通知所需的项目会突出显示,但在单击第二个通知时,前一个项目会再次突出显示。所以请帮忙。

代码:

@Override
protected void onNewIntent(Intent new_notification) {

caller = new_notification.getExtras().getString("CALLER");
if(caller.equals("GenNot")) {
int myScrollTo = new_notification.getExtras().getInt("ID");
Log.e("SEE NOW", "myScrollTo # "+myScrollTo, new Exception());
remindersList.requestFocusFromTouch();
remindersList.setSelection(myScrollTo);
}
}

通知代码:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;



public class GenerateNotification {

public static void reminderNotification(Context context, int notification_id, String document_id, String name, String date, Location location) {

Intent intent = new Intent(context, ViewReminders.class);
intent.putExtra("CALLER","GenNot");
intent.putExtra("ID", notification_id);
PendingIntent pIntent = PendingIntent.getActivity(context, notification_id, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setTicker("Smart Locator");
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle(name);
DetailsContainer dc = new LocationDetails(context).getDetails(location);
mBuilder.setContentText(date + ", " + dc.area + " " + dc.locality);
mBuilder.setContentIntent(pIntent).getNotification();
mBuilder.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(document_id, notification_id, mBuilder.build());
}

}

最佳答案

我在我的项目中遇到了同样的问题。通过调用 pendingIntent.cancel() 解决了这个问题收到后。

Map 中发布通知之前,我保存了指向 PendingIntent 的链接,然后使用 notification_id 找到了它

private final HashMap<String, PendingIntent> mIntents = new HashMap<String, PendingIntent>(); 
...
// on post
mIntents.put(challenge.getId(), pendingIntent);
...
// on click
if (mIntents.containsKey(challengeId)) {
mIntents.remove(challengeId).cancel();
}

official documentation中也有很好的解释:

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

There are two typical ways to deal with this.

If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

If you only need one PendingIntent active at a time for any of the Intents you will use, then you can alternatively use the flags FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT to either cancel or modify whatever current PendingIntent is associated with the Intent you are supplying.

关于android - 所需的 ListView 的项目未被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37367419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com