按下按钮时不停止的方法

时间:2017-10-14 11:16:01

标签: android

我是android新手。我做了一个简单的火炬灯应用程序。一切都很好,但闪烁的部分正在解决问题。如果我按下按钮"闪烁",手电筒开始闪烁但是当我再次按下按钮时,闪烁不会停止而不是闪烁加起来。例如,如果我按下按钮两次,则该功能执行两次。如果按顺序则必须在第二次按下时停止。任何人都有解决方案。请帮忙。

  

这是MainActivity.java

$PDOconnection = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $sql_username, $sql_password);

// Set PDO error mode to exception
$PDOconnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Set PDO Attribute to force native prepared statements
$PDOconnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  

这是我的activity_main.xml

package epicerastudios.torch;

import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;


public class MainActivity extends AppCompatActivity {

private CameraManager mCameraManager;
private String mCameraId;
private ImageButton mTorchOnOffButton;
private ImageButton mBlinkOnOffButton;
private Boolean isTorchOn;
private Boolean isBlinkOn;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("FlashLightActivity", "onCreate()");
    setContentView(R.layout.activity_main);
    mTorchOnOffButton = (ImageButton) findViewById(R.id.Switch);
    mBlinkOnOffButton = (ImageButton) findViewById(R.id.Blink) ;
    isTorchOn = false;
    isBlinkOn = false;

    Boolean isFlashAvailable = getApplicationContext().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!isFlashAvailable) {

        AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
                .create();
        alert.setTitle("Error !!");
        alert.setMessage("Your device doesn't support flash light!");
        alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // closing the application
                finish();
                System.exit(0);
            }
        });
        alert.show();
        return;
    }
    mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        mCameraId = mCameraManager.getCameraIdList()[0];
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

    mTorchOnOffButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                if (isTorchOn) {
                    turnOffFlashLight();
                    isTorchOn = false;
                } else {
                    turnOnFlashLight();
                    isTorchOn = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    mBlinkOnOffButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                if (isBlinkOn) {
                    turnOffBlink();
                    isBlinkOn = false;
                } else {
                    turnOnBlink();
                    isBlinkOn = true;
                }

        }
    });
}
public void turnOnFlashLight() {

    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mCameraManager.setTorchMode(mCameraId, true);

            mTorchOnOffButton.setImageResource(R.drawable.btn_on);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public void turnOffFlashLight() {

    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mCameraManager.setTorchMode(mCameraId, false);

            mTorchOnOffButton.setImageResource(R.drawable.btn_off);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void turnOnBlink() {
    mBlinkOnOffButton.setImageResource(R.drawable.blink_on);
    String myString = "0101010101";
    long blinkDelay = 300; //Delay in ms
    for (int i = 0; i < myString.length(); i++) {
        if (myString.charAt(i) == '0') {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mCameraManager.setTorchMode(mCameraId, true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
        else {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mCameraManager.setTorchMode(mCameraId, false);
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }

        try {
            Thread.sleep(blinkDelay);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

public void turnOffBlink() {

    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mCameraManager.setTorchMode(mCameraId, false);

            mBlinkOnOffButton.setImageResource(R.drawable.blink_off);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onStop() {
    super.onStop();
    if(isTorchOn || isBlinkOn){
        turnOffFlashLight();
        turnOffBlink();
    }
}


@Override
protected void onPause() {
    super.onPause();
    if(isTorchOn || isBlinkOn){
        turnOffFlashLight();
        turnOffBlink();
    }
}

@Override
protected void onResume() {
    super.onResume();
    if(isTorchOn || isBlinkOn){
        turnOnFlashLight();
        turnOnBlink();
    }
}
}

1 个答案:

答案 0 :(得分:0)

我认为这是因为for循环,再次按下按钮时没有停止。

public void turnOnBlink() {
mBlinkOnOffButton.setImageResource(R.drawable.blink_on);
String myString = "0101010101";
long blinkDelay = 300; //Delay in ms
for (int i = 0; i < myString.length(); i++) {
  if(!isBlinkOn) {
    break;
  }
    if (myString.charAt(i) == '0') {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mCameraManager.setTorchMode(mCameraId, true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

按钮

mBlinkOnOffButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

            if (isBlinkOn) {
                isBlinkOn = false;
                turnOffBlink();
            } else {
                isBlinkOn = true;
                turnOnBlink();
            }

    }
});