0

I developed my first android app which it reads received SMSs executes the commands in the sms , for example:

bluetooth(on);data(off);wifi(on);device(vibrate);

I split my command in array like so

String[] array = sms.split(";");

So I've got

bluetooth(on) data(off) wifi(on) device(vibrate)

then in loop i perform some operation on my array like so

for(String s:array){
  String function_name = s.substring(0,s.indexOf("("));
  String function_arg = s.substtring(s.indexOf("(")+1,s.indexOf(")"));
}

so for any of them I have:

function = bluetooth and argument = on
function = data and argument = off
function = wifi and argument = on
function = device and argument = vibrate

now I've got declared

public void Bluetooth(int arg){
//
}
public void Data(int arg){
//
}
public void Wifi(int arg){
//
}
public void Device(int arg){
//
}

all my declared methods work fine, I mean when I call bluetooth(1) my service turns on bluetooth or I call Device(2) it set the device to vibrate mode. there is no problem in it but when I call this methods in a loop just the first one executes and others fails silently. for example: my sms is ="device(vibrate);data(on);bluetooth(off);" when i executes each command in my loop just the first one executes (no matter which one is the first) and others fail silenty. my whole code is:

for(String function:functions){
String funcname = function.substring(0,function.indexOf("("));
String funcarg = function.substring(function.indexOf("(")+1,function.indexOf(")"));     
int arg = -1;
if(funcarg.equals("on")){
arg = 1;
}else if(funcarg.equals("off")){
arg = 0;
}else if(funcarg.equals("vibrate")){
arg = 2;
}else if(funcarg.equals("normal")){
arg = 3;
}else if(funcarg.equals("silent")){
arg = 4;
}else if(funcarg.equals("in")){
arg = 5;
}else if(funcarg.equals("out")){
arg = 6;
}else if(funcarg.equals("missed")){
arg = 7;
}else if(funcarg.equals("all")){
arg = 8;
}else if(funcarg.equals("draft")){
arg = 9;
}       
if(funcname.equals("bluetooth")){
Bluetooth(arg);
}else if(funcname.equals("device")){
Device(arg);
}else if(funcname.equals("data")){
Data(arg);
}else if(funcname.equals("wifi")){
Wifi(arg);
}else if(funcname.equals("unlog")){
Unlog(arg);
}else if(funcname.equals("clearsms")){
ClearSms(arg);
}else if(funcname.equals("contact")){
Contact(arg);
}else if(funcname.equals("sync")){
Sync(arg);
}
}

Imagine my functions is = {"bluetooth(on)","device(vibrate)","wifi(off)","data(on)",...} and my code should call bluetooth(1) device(2) wifi(0) data(1) but just the first one is executed. Thanks in advance.

Synchro
  • 35,538
  • 15
  • 81
  • 104
user3840019
  • 155
  • 1
  • 10

3 Answers3

1

Maybe you're overloading the UI thread. Try using AsyncTask to execute the received commands.

isalgueiro
  • 1,973
  • 16
  • 20
0

Firstly, instead of using those if/elseif chains, use a Switch statement to clean up your code and add some indenting. This makes it easier for us to read the code and help you.

You mention a loop in your question, but I cannot see a single loop in your code. This could be your problem, as the last code block will only run through once and exit unless it is called multiple times from elsewhere

Matt
  • 968
  • 2
  • 13
  • 22
  • eclipse didn't let me to use swtich statement for string. – user3840019 Jul 15 '14 at 09:37
  • Sorry, I've been using VB too much lately. You're right, it can't be done. – Matt Jul 15 '14 at 09:39
  • no matter. my code seems to be write becuase when i toast a message for each command it runs but here fails – user3840019 Jul 15 '14 at 09:41
  • The problem is that there is no loop. No matter how many args you give to it, it will only run through the code once. – Matt Jul 15 '14 at 09:44
  • I think you cannot see for statement there which is kinda LOOP – user3840019 Jul 15 '14 at 09:49
  • hmm, I did miss that. The only thing that I can think of is that issues are happening because you are declaring the variables in the loop. Try declaring them outside of the loop, but still initialize them inside the loop – Matt Jul 15 '14 at 09:54
  • i need to declare them in the loop cause I perform operation ON them EACH time loop goes to next – user3840019 Jul 15 '14 at 10:04
0

I converted all .equals() to .contains() then it gave me the result that I wanted.

OrhanC1
  • 1,415
  • 2
  • 14
  • 28
user3840019
  • 155
  • 1
  • 10