当前位置 : 主页 > 手机开发 > android >

android – 即使我用不同的数据创建了一些意图,我从通知中获得了相同的额外数

来源:互联网 收集:自由互联 发布时间:2021-06-11
我从这个 document复制并粘贴并尝试PutExtra. 我点击了button1,button2和button3,然后点击了button1的通知,但是使用button3启动了ResultActivity,为什么? 我希望显示为button1.你知道解决方案吗? publi
我从这个 document复制并粘贴并尝试PutExtra.

我点击了button1,button2和button3,然后点击了button1的通知,但是使用button3启动了ResultActivity,为什么?

我希望显示为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
    );
网友评论