Android Bundle额外发送错误的字符串
我正在为用户收到好友请求时生成通知。通知有一个接受和拒绝按钮,根据用户单击的内容,启动的活动将处理请求。无论如何,当用户单击“接受”时,启动的活动应该会收到一个名为“接受”的请求,反之亦然。然而,出于某种奇怪的原因,无论我单击“接受”还是“拒绝”,请求字符串都会返回“拒绝”。我甚至将拒绝请求字符串注释为on send accept或null,但它仍然返回拒绝。这太可笑了。这就是我所做的Android Bundle额外发送错误的字符串,android,android-intent,android-notifications,Android,Android Intent,Android Notifications,我正在为用户收到好友请求时生成通知。通知有一个接受和拒绝按钮,根据用户单击的内容,启动的活动将处理请求。无论如何,当用户单击“接受”时,启动的活动应该会收到一个名为“接受”的请求,反之亦然。然而,出于某种奇怪的原因,无论我单击“接受”还是“拒绝”,请求字符串都会返回“拒绝”。我甚至将拒绝请求字符串注释为on send accept或null,但它仍然返回拒绝。这太可笑了。这就是我所做的 public void newRequest(String name,String id, String re
public void newRequest(String name,String id, String relation){
Intent intent = new Intent(this, Contacts.class);
Bundle extras= new Bundle();
extras.putString("request", "accept");
extras.putString("relation", relation);
extras.putString("name", name);
extras.putString("id", id);
Intent in = new Intent(this, Contacts.class);
Bundle extra= new Bundle();
extra.putString("request", "decline");
extra.putString("relation", relation);
extra.putString("name", name);
extra.putString("id", id);
intent.putExtras(extras);
in.putExtras(extra);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
PendingIntent pIn = PendingIntent.getActivity(this, 0, in, 0);
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle(name+" sent you have received a friend request")
.setContentText("Reflap").setSmallIcon(R.drawable.fav)
.setContentIntent(pIntent)
.addAction(0, "Accept", pIntent)
.addAction(0, "Decline", pIn)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(5, noti);
}
这是接收意图的活动
Intent intent=getIntent();
//userName=intent.getStringExtra("username");
Bundle extras=intent.getExtras();
///*if(extras.getString("request")!=null){
try{
requester=extras.getString("request");
senderId=extras.getString("id");
namer=extras.getString("name");
relation=extras.getString("relation");
System.out.println("Starting to perform an accept with "+namer+" and user id "+senderId+" and request is "+requester);
}catch(Exception e){
e.printStackTrace();
}
但是每次我点击任何按钮,字符串都会返回decline。真奇怪 找到了答案。问题就在这里
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
PendingIntent pIn = PendingIntent.getActivity(this, 0, in, 0);
两个请求代码相同。它需要与众不同。这项工作:
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
PendingIntent pIn = PendingIntent.getActivity(this, 1, in, 0);
你是从哪里开始你的活动的?我不明白你的问题。此意图由alarmservice触发,该服务检查好友请求,如果有好友请求,则会触发通知,用户可以根据该通知采取行动。尝试将
pendingent.FLAG_UPDATE_CURRENT
传递到pendingent.getActivity
Yeah,从系统的角度来看,拥有不同的额外功能并不能使PendingEntities有足够的不同,因此旧的功能可以重用。另一种方法是在数据URI中附加一些唯一的字符串。我认为在不添加两个不同标志的情况下,您可以使用这一个PendingEvent.flag_CANCEL_CURRENT,而不是两个PendingEvent中的标志。非常感谢@Michael:)。。工作正常。。PendingEvent.getActivity(context,(int)System.currentTimeMillis(),minent,0);