Android 的 Bundle.putExtra 问题

发布于 2024-10-18 02:48:08 字数 3273 浏览 1 评论 0原文

我目前正在开发一个android应用程序,我在其中创建一个通知意图,它将参数添加到意图包中。单击通知时,它会调用一个活动并从包中获取数据。然而,第一次使用该应用程序时它工作正常,但是当您单击不同的项目时,它应该将不同的数据传递到通知活动中,但由于某种原因它不会用新数据替换旧数据。

在使用 putExtra 之前,我尝试调用 bundle.removeExtra("companyPassword") 但它似乎没有任何区别。下面是通知的代码

private void notification(String companyName, String companyURL, String companyUsername, String loginPassword)
{
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

    int icon = R.drawable.icon;
    CharSequence tickerText = "Click notification to copy password";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "PM - Login Management";
    CharSequence contentText = "Click here to copy password";
    Intent notificationIntent = new Intent(ShowLogins.this, DataManagement.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    notificationIntent.removeExtra("companyName");
    notificationIntent.removeExtra("companyURL");
    notificationIntent.removeExtra("companyUsername");
    notificationIntent.removeExtra("companyPassword");
    notificationIntent.putExtra("companyName", companyName);
    notificationIntent.putExtra("companyURL", companyURL);
    notificationIntent.putExtra("companyUsername", companyUsername);
    notificationIntent.putExtra("companyPassword", loginPassword);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    final int NOTIFICATION_ID = 1;

    notification.defaults |= Notification.DEFAULT_SOUND;
    //notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(NOTIFICATION_ID, notification);
    finish();

下面是通知活动的代码,它检索传递到包中的数据

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            // setContentView(R.layout.data_mangement);

            Bundle bundle = this.getIntent().getExtras();

            company = bundle.getString("companyName");
            companyURL = bundle.getString("companyURL");
            username = bundle.getString("companyUsername");
            password = bundle.getString("companyPassword");
            bundle.clear();

            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            Encryption encryption = new Encryption();
            String decryptedPassword = encryption.decrypt(password);
            clipboard.setText(decryptedPassword);
            toastNotification("Password Successfully Copied\n\nPaste into password field");
            bundle.clear();
            finish();
            moveTaskToBack(true);

        } catch (Exception ex) {
            Log.d("DataManagement Error", ex.toString());
        }
    }

由于某种原因,当调用通知的活动时,它只返回第一次发送的数据首先选择项目而不是检索新数据。

感谢您提供的任何帮助。

I am currently developing an android app where I create a notification Intent which adds parameters to the bundle of the intent. When the notification is clicked it calls an activity and gets the data from the bundle. However, the first time the app is used it works fine, but when you click on a different item its supposed to pass different data onto the notification activity but for some reason its not replacing the old data with the new.

I have tried to call bundle.removeExtra("companyPassword") before I use the putExtra but it doesn't seem to make any difference. Below is the code for the notification

private void notification(String companyName, String companyURL, String companyUsername, String loginPassword)
{
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

    int icon = R.drawable.icon;
    CharSequence tickerText = "Click notification to copy password";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "PM - Login Management";
    CharSequence contentText = "Click here to copy password";
    Intent notificationIntent = new Intent(ShowLogins.this, DataManagement.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    notificationIntent.removeExtra("companyName");
    notificationIntent.removeExtra("companyURL");
    notificationIntent.removeExtra("companyUsername");
    notificationIntent.removeExtra("companyPassword");
    notificationIntent.putExtra("companyName", companyName);
    notificationIntent.putExtra("companyURL", companyURL);
    notificationIntent.putExtra("companyUsername", companyUsername);
    notificationIntent.putExtra("companyPassword", loginPassword);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    final int NOTIFICATION_ID = 1;

    notification.defaults |= Notification.DEFAULT_SOUND;
    //notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(NOTIFICATION_ID, notification);
    finish();

And below is the code for the Notification activty where it retrieves the data that is passed into the bundle

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            // setContentView(R.layout.data_mangement);

            Bundle bundle = this.getIntent().getExtras();

            company = bundle.getString("companyName");
            companyURL = bundle.getString("companyURL");
            username = bundle.getString("companyUsername");
            password = bundle.getString("companyPassword");
            bundle.clear();

            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            Encryption encryption = new Encryption();
            String decryptedPassword = encryption.decrypt(password);
            clipboard.setText(decryptedPassword);
            toastNotification("Password Successfully Copied\n\nPaste into password field");
            bundle.clear();
            finish();
            moveTaskToBack(true);

        } catch (Exception ex) {
            Log.d("DataManagement Error", ex.toString());
        }
    }

For some reason when the activity for the notification is called it is only returning the data that was first sent when an item was first selected instead of retrieving the new data.

Thanks for any help you can provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2024-10-25 02:48:08

但是,第一次使用该应用程序时它工作正常,但是当您单击不同的项目时,它应该将不同的数据传递到通知活动中,但由于某种原因它不会用新数据替换旧数据。

这是因为您没有充分更改您的 Intent 并且您没有使用 FLAG_UPDATE_CURRENT 在您的 PendingIntent 中。尝试后者,看看是否有帮助。

However, the first time the app is used it works fine, but when you click on a different item its supposed to pass different data onto the notification activity but for some reason its not replacing the old data with the new.

That is because you are not sufficiently changing your Intent and you are not using FLAG_UPDATE_CURRENT in your PendingIntent. Try the latter and see if it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文