1

im new in android and java & i have this intent

String temp  = intent.getStringExtra("msg");

and it return multiple word continuously like "msg a" , "msg b" , "msg c" ...and i want to store all these words to another string

this is what i tried

String anothertemp = temp;

but the problem is it just store last word "msg c". i mean it will replace and overwrite new word with old words

i want to store all these words in one new string and each word in new line like this

msg a
msg b 
msg c

by the way i allready tried these too

String temp  = intent.getStringExtra("msg")+ "\n" ;
&
String anothertemp = temp+ "\n";

still have the same problem. sorry for my poor english

EDIT ONE

this is my 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++;
            }

        }

    }
}

and this is how i call it

class NotificationReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        String temp = intent.getStringExtra("msg") + "\n";
String anothertemp = temp;
    }
}

Edit two

consider that we have 3 notification "msg1","msg2" and "msg3" ,in method onReceive in the NotificationReceiver class will post the string one by one (one after another )when the msg1 string posted then it will post msg2 string. it wont post all this msg's together in same time .. and this is what will happened ; it will post notification msg 1 string and then will post msg2 string and it will be replace and overwrtie on msg 1 ,,and then it will post msg 3 and it will be replace and overwrite on msg2...so in the end i only can get msg 3 string...

thanks for any help

m.r.davari
  • 164
  • 1
  • 10
  • do you mean like `anothertemp += temp;`? – Orin Sep 14 '16 at 17:41
  • @Orin2005 no,i mean i want all intent's string msg's in one string – m.r.davari Sep 14 '16 at 17:49
  • Could you say a bit more about the contents of `intent`? Are your messages always delimited by a constant sub-string like "msg"? – markspace Sep 14 '16 at 17:56
  • @markspace i add intent in my edit please check it ..its part of notification listener . you can find it here [link](https://github.com/kpbird/NotificationListenerService-Example) – m.r.davari Sep 14 '16 at 18:18

2 Answers2

1

String x = "Hello," + System.lineSeparator() + "there";

for other solutions check this out Java String new line

Community
  • 1
  • 1
Ritesh
  • 1,030
  • 2
  • 12
  • 28
  • i know how to insert new line but my problem is string will be replace and old one will lost.. i need to avoid replacing words – m.r.davari Sep 14 '16 at 18:08
1

When appending to the old string a new one, we use += operator. So you can use

String str = "foo";
str += "bar"; //the value of str will be 'foobar' now

Although, you can face something called variable scope. For example, the variables temp and anotherTemp in your method onReceive in the NotificationReceiver class will be "emptied" and rewritten each time the method gets called.

To prevent from this you will have to move the variables out of the method. Making them instance variables might do the work. See this link for more information about variable scopes.

  • thanks but my problem is some thing else.. consider that we have 3 notification "msg1","msg2" and "msg3" ,in method onReceive in the NotificationReceiver class will post the string one by one (one after another )when the msg1 string posted then it will post msg2 string. it wont post all this msg's together in same time .. and this is what will happened ; it will post notification msg 1 string and then will post msg2 string and it will be replace and overwrtie on msg 1 ,,and then it will post msg 3 and it will be replace and overwrite on msg2...so in the end i only can get msg 3 string... – m.r.davari Sep 14 '16 at 19:14
  • Denis is absolutely correct. make your string variable an instance variable or static variable. – Harsh kurra Sep 15 '16 at 13:10