gpt4 book ai didi

java - android通知适用于循环中的一个对象

转载 作者:行者123 更新时间:2023-12-01 18:48:03 24 4
gpt4 key购买 nike

我有一个 android 服务,它使用 Web 服务连接到服务器,该服务返回许多 json 对象,我想为每个对象运行通知,我做了所有这些,但我有一个问题,那就是我什至只看到一个通知认为服务器正在发送两个对象。请查看for循环,很明显我调用了很多通知,为什么我只看到一个?

public class GetOffersService extends Service {

@Override
public void onCreate() {
super.onCreate();
Client client = new Client("http://" + Configuration.hostIP
+ ":8080/test2/ssssss/");
String str = client.getBaseURI("offers");
try {
JSONArray json = new JSONArray(str);
for (int i = 0; i < json.length(); i++) {
JSONObject oneOffer = json.getJSONObject(i);
int offerID = oneOffer.getInt("ID");
String offerDescriptoin = oneOffer.getString("Description");
String endDate = oneOffer.getString("EndDate");
String startDate = oneOffer.getString("StartDate");
JSONObject restaurant = oneOffer.getJSONObject("Restaurant");
int restaruantID = restaurant.getInt("ID");
String restaurantName = restaurant.getString("Name");
Intent intent = new Intent(this, OfferNotification.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,
intent, 0);
Uri soundUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.ic_launcher, "call", pIntent)
.addAction(R.drawable.ic_launcher, "more", pIntent)
.addAction(R.drawable.ic_launcher, "add more", pIntent)
.setContentTitle("The Eattel")
.setContentText("New Offer!").setSound(soundUri);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, OfferNotification.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

stackBuilder.addParentStack(OfferNotification.class);

stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Signin.NOTIFICATION_SERVICE);

mNotificationManager.notify(100, mBuilder.build());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

}

最佳答案

请阅读此处的“堆栈通知”章节: http://developer.android.com/design/patterns/notifications.html

如果您的应用创建了一个通知,而另一个相同类型的通知仍处于待处理状态,请避免创建全新的通知对象。相反,堆叠通知。

堆叠通知构建摘要描述,并允许用户了解有多少特定类型的通知正在等待处理。

不要:

enter image description here

做:

enter image description here

您可以使用扩展的摘要布局提供有关构成堆栈的各个通知的更多详细信息。这使用户可以更好地了解哪些通知正在等待处理,以及这些通知是否足够有趣,可以在关联的应用程序中详细阅读。

enter image description here

关于java - android通知适用于循环中的一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16881045/

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