不能让我的手电筒工作多次 - 安卓

时间:2015-03-27 12:32:50

标签: android flashlight android-imagebutton

大家好,我正在尝试为Android手机构建我的第一个应用程序。这是一个具有一些功能的手电筒。我已经编写了On / Off按钮的代码但是使用我的exigisting代码,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;
    }
    }
}

1 个答案:

答案 0 :(得分:0)

turnOffFlash()方法中更改此

isFlashOff = false;

isFlashOn = false;

您没有在任何地方使用isFlashOff

删除所有出现的varaibale

boolean isFlashOff;