Android 的 Bundle.putExtra 问题
我目前正在开发一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您没有充分更改您的
Intent
并且您没有使用FLAG_UPDATE_CURRENT
在您的PendingIntent
中。尝试后者,看看是否有帮助。That is because you are not sufficiently changing your
Intent
and you are not usingFLAG_UPDATE_CURRENT
in yourPendingIntent
. Try the latter and see if it helps.