- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 android 和 java 的新手,我有这个 Intent
String temp = intent.getStringExtra("msg");
并连续返回多个单词像 "msg a", "msg b", "msg c"...我想把所有这些词存储到另一个字符串中
这是我试过的
String anothertemp = temp;
但问题是它只存储了最后一个词“msg c”。我的意思是它会用旧词替换和覆盖新词
我想将所有这些单词存储在一个新字符串中,每个单词都像这样在新行中存储
msg a
msg b
msg c
顺便说一句,我也已经尝试过了
String temp = intent.getStringExtra("msg")+ "\n" ;
&
String anothertemp = temp+ "\n";
还是一样的问题。抱歉我的英语不好
编辑一个
这是我的 Intent
class NLServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra("command").equals("list")){
int i=1;
for (StatusBarNotification sbn : NLService.this.getActiveNotifications()) {
Intent i2 = new Intent("notificationmsg");
i2.putExtra("msg",i +" " + sbn.getPackageName() + "\n");
sendBroadcast(i2);
i++;
}
}
}
}
我是这样调用它的
class NotificationReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String temp = intent.getStringExtra("msg") + "\n";
String anothertemp = temp;
}
}
编辑二
考虑到我们有 3 个通知“msg1”、“msg2”和“msg3”,在 NotificationReceiver 类的 onReceive 方法中将一个接一个地(一个接一个)发布字符串,当 msg1 字符串发布时,它将发布 msg2字符串。它不会同时发布所有这些消息。这就是将要发生的事情;它将发布通知 msg 1 字符串,然后将发布 msg2 字符串,它将在 msg 1 上被替换和覆盖,然后它会发布 msg 3,它将在 msg2 上被替换和覆盖...所以最后我只可以获取 msg 3 字符串...
谢谢你的帮助
最佳答案
当向旧字符串追加新字符串时,我们使用 += 运算符。所以你可以使用
String str = "foo";
str += "bar"; //the value of str will be 'foobar' now
不过,您可能会遇到称为变量作用域的问题。例如,NotificationReceiver
类中的方法 onReceive
中的变量 temp
和 anotherTemp
将被“清空”并且每次调用该方法时都会重写。
为防止这种情况发生,您必须将变量移出方法。使它们成为实例变量可能会起作用。参见 this link有关变量范围的更多信息。
关于java - 如何在一个字符串中存储多个单词而不替换java中的旧单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39496552/
我是一名优秀的程序员,十分优秀!