不能让我的手电筒工作不止一次 - android

Cant make my flashlight work more than one time - android

大家好,我正在尝试为 android 手机构建我的第一个应用程序。这是一款具有某些功能的手电筒。我已经为 On/Off 按钮编写了代码,但是使用我现有的代码 On/Off 按钮只能工作一次,我不知道如何修复它才能多次工作。这是我的代码:

public class MainActivity extends ActionBarActivity {

ImageButton OnOff;
Button SOS, Pallette;
private Camera camera;
boolean isFlashOn;
boolean isFlashOff;
boolean hasFlash;
Parameters params;
MediaPlayer mp;
String blink = "01010101";
long blinkDelay = 50;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // declare flash switch button
    OnOff = (ImageButton) findViewById(R.id.bOnOff);

    // declare sos flash button
    SOS = (Button) findViewById(R.id.bSOS);

    // Now we check if our device has flash so we can turn on
    hasFlash = getApplicationContext().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash) {
        // if our device doesn't have flash we show an error message
        // after pressing "OK" on error message we close the application

        AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
                .create();
        alert.setTitle("Error");
        alert.setMessage("Sorry but your device doesn't support flash");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        alert.show();
        return;
    }

    // Now we get the Camera
    getCamera();

    // Display button image
    toggleButtonImage();

    OnOff.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isFlashOn) {
                // turn off flash
                turnOffFlash();
            } else {
                // turn on flash
                turnOnFlash();
            }
        }
    });

private void getCamera() {
    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        } catch (RuntimeException e) {
            Log.e("Camera Error", e.getMessage());
        }
    }
}

private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;
    }

    toggleButtonImage();
}

private void turnOffFlash() {
    if (isFlashOn) {
        if (camera == null || params == null) {
            return;
        }

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();
        isFlashOff = false;
    }

    toggleButtonImage();
}

private void toggleButtonImage() {
    if (isFlashOn) {
        OnOff.setImageResource(R.drawable.on);
    } else {
        OnOff.setImageResource(R.drawable.off);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
protected void onRestart() {
    super.onRestart();
}

@Override
protected void onStop() {
    super.onStop();

    if (camera != null) {
        camera.release();
        camera = null;
    }
    }
}

turnOffFlash()方法中改变这个

isFlashOff = false;

进入

isFlashOn = false;

您没有在任何地方使用isFlashOff

删除所有出现的变量

boolean isFlashOff;