我一直在为Android制作一个手电筒应用程序,并遇到了几个问题,并且已经能够解决这些问题。但是现在当我运行应用程序时,它会加载到我的HTC上并运行正常,直到我第四次按下手电筒电源按钮。我可以按一下它就会打开。我第二次按它然后关闭。我第三次按它然后重新开启。 我按下它第四个并且它不会关闭?帮助吗
public class MainActivity extends Activity {
ImageButton powerButton;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
@TargetApi(Build.VERSION_CODES.ECLAIR)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
powerButton = (ImageButton) findViewById(R.id.power_button);
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
.create();
alert.setTitle("Notice");
alert.setMessage("I'm sorry, your device doesn't support flashlight!");
alert.setButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
return;
}
getCamera();
toggleButtonImage();
powerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
turnOffFlash();
} else {
turnOnFlash();
}
}
});
}
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Failed to open camera. Alert: ", e.getMessage());
}
}
}
public 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();
isFlashOn = false;
toggleButtonImage();
}
}
private void toggleButtonImage(){
if(isFlashOn){
powerButton.setImageResource(R.drawable.flashlight_on);
}else{
powerButton.setImageResource(R.drawable.flashlight_off);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
turnOffFlash();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onResume() {
super.onResume();
if(hasFlash)
turnOffFlash();
}
@Override
protected void onStart() {
super.onStart();
getCamera();
}
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
camera = null;
}
}
' 清单 - http://pastebin.com/Tw6LTemP ' Activity_main.xml - http://pastebin.com/Tga1agN7
答案 0 :(得分:0)
至于你所描述的症状,可能是SurfaceView崩溃了。请从logcat发布您的错误日志。
另外,你在onResume()中的代码对我来说没有意义(虽然,这不是你问题的直接原因,但问题的直接原因,请再次看你的logcat):
@Override
protected void onResume() {
super.onResume();
if(hasFlash)
turnOffFlash();
}
我想你是想说if(isFlashOn)turnOffFlash();但即使你纠正它,那仍然是错误的。
答案 1 :(得分:0)
我用来关闭手电筒的代码是:
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
}
}