我有一个使用 Web 服务连接到服务器的 android 服务,该服务返回许多 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;
}
}