Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundle.putExtra problem with android

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.

like image 623
Boardy Avatar asked Feb 19 '11 15:02

Boardy


1 Answers

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.

like image 160
CommonsWare Avatar answered Sep 19 '22 08:09

CommonsWare