我正在制作一款应用程序,在我的Android应用程序中使用无触摸手势关闭和关闭闪光灯。它打开就好了,但我无法让它关闭。在此先感谢您的帮助。
这是我的MainActivity.java
package com.proximitysensorapp.primo.app;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
import java.io.IOException;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
public class MainActivity extends Activity implements SensorEventListener, Callback
{
SensorManager sm;
Sensor s;
TextView et1;
Intent Activitynew;
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
public void TurnOn(View view)
{
if (FlashAvailable() && camera != null)
{
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
//camera.startPreview();
try
{
camera.setPreviewDisplay(surfaceHolder);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void TurnOff(View view) {
if (FlashAvailable() && camera != null) {
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
//camera.stopPreview();
}
}
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
s = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
Activitynew = new Intent(MainActivity.this, Activity2.class);
surfaceView = (SurfaceView) this.findViewById(R.id.surface1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void onResume()
{
super.onResume();
try {
camera = Camera.open();
} catch (Exception e) {
//---exception handling here---
Log.d("Flashlight", e.toString());
}
}
@Override
public void onPause() {
super.onPause();
TurnOff(null);
camera.release();
}
private Boolean FlashAvailable() {
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
}
@Override
public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY)
{
int temp = (int) event.values[0];
if (temp==1)
{
TurnOff(null);
}
else if (temp==0)
{
TurnOn(null);
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
你应该添加
mCamera.stopPreview();
mCamera.release();
在你的关闭方法中