【发布时间】:2014-07-15 09:18:33
【问题描述】:
我开发了我的第一个 android 应用程序,它读取收到的短信执行短信中的命令,例如:
bluetooth(on);data(off);wifi(on);device(vibrate);
我像这样将我的命令拆分成数组
String[] array = sms.split(";");
所以我有
bluetooth(on) data(off) wifi(on) device(vibrate)
然后在循环中我像这样对我的数组执行一些操作
for(String s:array){
String function_name = s.substring(0,s.indexOf("("));
String function_arg = s.substtring(s.indexOf("(")+1,s.indexOf(")"));
}
所以对于其中任何一个我都有:
function = bluetooth and argument = on
function = data and argument = off
function = wifi and argument = on
function = device and argument = vibrate
现在我已经宣布了
public void Bluetooth(int arg){
//
}
public void Data(int arg){
//
}
public void Wifi(int arg){
//
}
public void Device(int arg){
//
}
我声明的所有方法都可以正常工作,我的意思是当我调用蓝牙(1)时,我的服务会打开蓝牙,或者我调用设备(2)时,它会将设备设置为振动模式。没有问题,但是当我在循环中调用此方法时,只有第一个方法会执行,而其他方法会静默失败。例如: 我的短信是 =“设备(振动);数据(开启);蓝牙(关闭);” 当我在循环中执行每个命令时,只会执行第一个命令(无论哪个是第一个),而其他命令则静默失败。 我的整个代码是:
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);
}
}
想象一下我的函数是 = {"bluetooth(on)","device(vibrate)","wifi(off)","data(on)",...}
我的代码应该调用bluetooth(1) device(2) wifi(0) data(1),但只执行第一个。提前致谢。
【问题讨论】:
标签: java android loops methods