Android 意图没有正确传达
我正在通过PendingEvent从GCMinentService发送意向。我在intent中插入了一些字符串,在pendingent中,我调用了一个将接收intent的活动。但数据不是我要发送的。这是截图。 显示的第一个意图是我正在发送的,第二个意图是我正在接收的。以下是发送的代码:Android 意图没有正确传达,android,Android,我正在通过PendingEvent从GCMinentService发送意向。我在intent中插入了一些字符串,在pendingent中,我调用了一个将接收intent的活动。但数据不是我要发送的。这是截图。 显示的第一个意图是我正在发送的,第二个意图是我正在接收的。以下是发送的代码: Intent intent = new Intent(getBaseContext(), Group_Chat_MainActivity.class);
Intent intent = new Intent(getBaseContext(),
Group_Chat_MainActivity.class);
intent.putExtra("group_id", group_id);
intent.putExtra("group_title", name);
intent.putExtra("from_push", true);
PendingIntent pendingIntent = PendingIntent.getActivity(
getBaseContext(), 100,
intent, 0);
Notification notification = new NotificationCompat.Builder(
getBaseContext()).setContentTitle("Paisa Swipe")
.setContentText(msg)
.setSmallIcon(R.drawable.home_logo)
.setTicker(ticker)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setAutoCancel(true).build();
notificationManager.notify(Integer.parseInt(group_id),
notification);
当我单击notification时,它会发送到Group_Chat_MainActivity类。在这种情况下,我使用getIntent()获取Intent数据:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getIntent().getStringExtra("group_title")
getIntent().getStringExtra("group_id")
getIntent().getBooleanExtra("from_push",false)
}
在这里,我得到了不同的字符串,然后我发送。有什么想法吗?确保您的
getBaseContext()
不为空。如果您在Fragmnet中使用它,请尝试:
inflatedview.getContext();
而不是:
getBaseContext()
getIntent().getStringExtra();
当您想在OnCreate
方法中从您的意图中获得价值时,请尝试以下方法:
getIntent().getExtras().getString();
而不是:
getBaseContext()
getIntent().getStringExtra();
希望这对您有所帮助。事实上,问题是您的PendingEvent请求代码始终相同,即100。将其保留为组的唯一内容,例如您的组id。此处的上下文也应为您的GCMinentService上下文。请尝试将其更改为
PendingIntent pendingIntent = PendingIntent.getActivity(
getBaseContext(), 100,
intent, 0);
这个
您确定您的
getBaseContext()
不为空吗?这段代码在一个片段中?你错在哪一段value@NoName组标题返回奇怪的值“测试组”。我搜索了我的整个项目,甚至在代码或xmls:PTry中都没有名为“test”的字符串,在构建PendingEvent(而不是0)时使用了“PendingEvent.FLAG_UPDATE_CURRENT”标志。首先,它是一个服务,而不是一些视图。其次,getIntent().getExtras().getString()是去润滑的。@Prince我在If
语句中提到过它,您能解释一下或者提供一些链接吗:“getIntent().getExtras().getString()是去润滑的。