意图没有正确传递

时间:2014-11-24 13:35:25

标签: android

我通过PendingIntent从GCMIntentService发送一个Intent。我在意图和PendingIntent中插入一些字符串,我调用一个将接收意图的活动。但数据不是我发送的。这是截图。 enter image description here

显示的第一个Intent是我发送的内容,第二个是我收到的内容。以下是发送代码:

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);

当我点击通知时,它会发送到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)
    }

在这里,我收到了不同的字符串然后我发送的内容。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

实际上问题是你的PendingIntent requestCode始终是相同的,即100。保持它是一个独特的组,例如。你的group_id。此处的上下文也应该是您的GCMIntentService上下文。请尝试将其更改为

PendingIntent pendingIntent = PendingIntent.getActivity(
                        getBaseContext(), 100,
                        intent, 0);

PendingIntent pendingIntent = PendingIntent.getActivity(
                        yourContextHere, Integer.parseInt(group_id),
                        intent, 0);

答案 1 :(得分:0)

确保getBaseContext()不为空。
如果你在Fragmnet中使用它,请尝试:

inflatedview.getContext();

而不是:

getBaseContext()

如果您希望通过OnCreate方法获取您的意图值,请尝试以下操作:

getIntent().getExtras().getString();

而不是:

getIntent().getStringExtra();

希望这对你有所帮助。