Bundle.putExtra与android的问题

时间:2011-02-19 15:39:45

标签: android bundle android-intent

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

在我使用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();

以下是Notification activty的代码,它检索传递到包中的数据

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

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

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:6)

  

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

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