android - 即使我使用不同的数据创建一些 Intent ,我也会从通知中获得相同的额外数据

标签 android android-intent android-notifications android-pendingintent

我从这个 document 复制并粘贴了并尝试 PutExtra

我点击了 button1button2button3,然后点击了来自 button1 的通知,但是 ResultActivity 是从 button3 开始的,为什么?

我想显示为 button1。你知道解决方案吗?

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(listener);
        findViewById(R.id.button2).setOnClickListener(listener);
        findViewById(R.id.button3).setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    notifyNotification("button1");
                    break;
                case R.id.button2:
                    notifyNotification("button2");
                    break;
                case R.id.button3:
                    notifyNotification("button3");
                    break;
            }

        }
    };

    private int mId = 0;

    private void notifyNotification(String value) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(android.R.drawable.stat_notify_chat)
                        .setContentTitle("My notification")
                        .setContentText(value);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, ResultActivity.class);
        resultIntent.putExtra("value", value); // ***** I added this code *****

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(ResultActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());

        mId++;
    }
}

public class ResultActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        Intent intent = getIntent();
        String value = intent.getStringExtra("value");

        ((TextView)findViewById(R.id.textView)).setText(value);
    }
}

最佳答案

问题在于 PendingIntent.FLAG_UPDATE_CURRENT 并使用相同的 PendingIntent 请求代码,即 0。这样,所有通知的 Intent 将更新为最后一次按下的 Button

也尝试为每个 PendingIntent 使用不同的请求代码。

PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(
        mId, // update to use same ID with notification's ID
        PendingIntent.FLAG_UPDATE_CURRENT
    );

关于android - 即使我使用不同的数据创建一些 Intent ,我也会从通知中获得相同的额外数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22004098/

相关文章:

Android 通知重建而不是更新?

android - 查看其他应用程序的通知

java - Android Studio 3.3.1 Gradle 4.10.1使项目构建失败

java - 单击同一按钮时,如何在一项 Activity 中显示不同的内容

android - 无法获取从服务类发送的 Activity 中的字符串

Android Phonegap GCM 多重通知

android - 如何在android中使用ffmpeg反转视频?

android - 带有自定义 ActionBar 的半透明 ImageSlider

android - 如何检查一个 Activity 是否活跃?

android - "no activity found to handle intent"并非所有手机都安装了Android Market