Android 多次执行一段代码

Android 多次执行一段代码,android,gps,sms,Android,Gps,Sms,我正在开发一个应用程序,我只想在bluetoothspeed值更改并超过该值时发送一次SMS。 这是我正在使用的代码-每次超过bluetoothspeed+30值时,它都会发送SMS if (GPSspeed > bluetoothSpeed+30) { if (!malreadyExecuted) { Log.d("malreadyExecuted", " in if block is = " + String.valueOf(m

我正在开发一个应用程序,我只想在bluetoothspeed值更改并超过该值时发送一次SMS。
这是我正在使用的代码-每次超过bluetoothspeed+30值时,它都会发送SMS

  if (GPSspeed > bluetoothSpeed+30) { if (!malreadyExecuted) {
                           Log.d("malreadyExecuted", " in if block is = " + String.valueOf(malreadyExecuted));
                           sendsms();
                           malreadyExecuted = true;}}

                           malreadyExecuted = false ;}

将malreadyExecuted设置为false的位置。如果在下一行,则它将不起作用。

您忘记告诉我们问题出在哪里。问题是,当gpsspeed高于bluetoothspeed+30时,我希望sms只发送一次,然后当我更改bluetoothspeed值时,sms应发送一次,依此类推,如果在If阻塞后将其设置为false,则确定,那么它就错了。我将其设置为false,以便在bluetoothspeed每次更改并且正常工作时发送短信;现在,当GPSspeed超过bluetoothspeed+30值时,我希望它只发送一次。我希望检查您现在使用的else块是否完整。正如您之前提到的,确实存在一些逻辑错误。请尝试{Scanner in=new Scanner(cmd)。使用delimiter([^0-9]+”;bluetoothSpeed=in.nextInt();if(GPSspeed>bluetoothSpeed&(bluetoothSpeed!=lastbuetoothspeed | | GPSspeed!=lastpsspeed)){lastBluetoothSpeed=bluetoothSpeed;lastGPSspeed=GPSspeed;lastBluetoothTime=System.currentTimeMillis()/1000L;}否则如果(bluetoothSpeed!=lastBluetoothSpeed){lastBluetoothSpeed=bluetoothSpeed;}否则如果(lastBluetoothSpeed!=-1&&GPSspeed>lastBluetoothSpeed&&GPSspeed!=lastGPSspeed){lastBluetoothTime=System.currentTimeMillis()/1000L;