手电筒应用程序恢复不起作用?



嘿伙计们,我正在使用在线教程开发手电筒应用程序。我对应用程序进行了一些更改。我希望如果用户进入主屏幕或锁定屏幕,手电筒保持打开状态。但是,当我执行此操作并打开默认相机应用程序,然后再次打开我的手电筒应用程序时,手电筒不再工作。

重现错误的步骤:

  1. 打开应用程序(自动转动闪光灯)
  2. 按主屏幕按钮(闪光灯保持开启)
  3. 打开默认相机应用程序(闪光灯关闭)
  4. 再次打开手电筒应用程序(开/关按钮不起作用,闪光灯保持关闭)

我的代码:

package ali.simpleflaslight;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton btnSwitch;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
//================================== Flash Switch Button =============================//
btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
//================================== First Check =====================================//
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
.create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support a flashlight!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
alert.show();
return;
}
getCamera();
toggleButtonImage();
btnSwitch.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
} catch (Exception e) {}
}
//====================================== Getting Camera Parameters ===========================//
private void getCamera() {
try {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (Exception e) {}
}
} catch (Exception e) {}
}
//====================================== Turning On Flash ====================================//
private void turnOnFlash() {
try {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
//Play Sound
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
//Changing Button/Switch Image
toggleButtonImage();
}
} catch (Exception e) {}
}
//====================================== Turning Off Flash ===================================//
private void turnOffFlash() {
try {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
//Play Sound
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
//Changing Button/Switch Image
toggleButtonImage();
}
} catch (Exception e) {}
}
//====================================== Toggle Image ========================================//
private void toggleButtonImage() {
try {
if (isFlashOn) {
btnSwitch.setImageResource(R.drawable.btn_switch_on);
} else {
btnSwitch.setImageResource(R.drawable.btn_switch_off);
}
} catch (Exception e) {}
}
//====================================== Play Sound ==========================================//
private void playSound() {
try {
if (isFlashOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
} catch (Exception e) {}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
//================================== On Pause Turn Off The Flash =========================//
//turnOffFlash();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onResume() {
super.onResume();
//================================== On Resume Turn On The Flash =========================//
if (hasFlash) {
turnOnFlash();
}
}
@Override
protected void onStart() {
super.onStart();
//================================== On Starting The App Get The Camera Params============//
getCamera();
}
@Override
protected void onStop() {
super.onStop();
// on stop release the camera
/*if (camera != null) {
camera.release();
camera = null;
}*/
}
}

在我注释 onStop 中的代码后,该应用程序停止工作。令人惊讶的是,Playstore中所有支持背景闪光灯的顶级手电筒应用程序,如果按照上述步骤进行操作,就会停止工作。然而,对我来说,该应用程序只是停止做任何事情,因为我尝试了流行语,我猜。有什么解决方案吗?

编辑:

将getCamera放在onResume之前后出错(并执行与上述相同的步骤):

05 - 08 19: 41: 10.955 4378 - 4378 / ali.simpleflaslight W / System.err﹕ java.lang.RuntimeException: getParameters failed(empty parameters)
05 - 08 19: 41: 10.955 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at android.hardware.Camera.native_getParameters(Native Method)
05 - 08 19: 41: 10.955 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at android.hardware.Camera.getParameters(Camera.java: 2075)
05 - 08 19: 41: 10.955 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at ali.simpleflaslight.MainActivity.turnOffFlash(MainActivity.java: 114)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at ali.simpleflaslight.MainActivity.access$100(MainActivity.java: 15)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at ali.simpleflaslight.MainActivity$2.onClick(MainActivity.java: 56)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at android.view.View.performClick(View.java: 4764)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at android.view.View$PerformClick.run(View.java: 19833)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at android.os.Handler.handleCallback(Handler.java: 739)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at android.os.Handler.dispatchMessage(Handler.java: 95)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at android.os.Looper.loop(Looper.java: 135)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at android.app.ActivityThread.main(ActivityThread.java: 5292)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at java.lang.reflect.Method.invoke(Method.java: 372)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 908)
05 - 08 19: 41: 10.956 4378 - 4378 / ali.simpleflaslight W / System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 703)

首先,你有try / catch块,其中catch(Exception e)块绝对不做任何事情 -永远不要这样做 - 这可能是为异常编程的最糟糕的方法之一。至少将e.printStackTrace()添加到catch块中,然后监视logcat的异常 - 我很确定您会发现一些被记录。

其次,一次只能有一个应用程序使用相机。最佳做法是onPause()方法release()相机,然后在onResume()中再次open()相机。由于您希望手电筒保持打开状态,因此您可能认为不release()相机是可以接受的 - 内置相机几乎肯定会遵循最佳实践,并且您的手电筒将在呼叫open()时熄灭,然后它会release()将相机留在"重置"阶段。

尝试从onCreate(...)方法中删除对getCamera()的调用,并将其放入onResume()(在调用turnOnFlash()之前)。

相关内容

  • 没有找到相关文章

最新更新