感应动作时打开/关闭手电筒

时间:2015-06-09 17:18:20

标签: android surfaceview flashlight

我正在开发一款Android应用,其中当手机震动时手电筒会开启/关闭。我被困在表面查看部分。如何检测SurfaceHolder.callback覆盖的方法中的运动?

我可以通过按钮成功地在没有动作检测的情况下打开和关闭手电筒。但我在同步运动检测和表面视图方面遇到了麻烦。

这是我的代码:

 public class FlashActivity extends Activity implements SurfaceHolder.Callback {

ImageView image;
TextView text;
int flag = 0, count = 0;
long lastShakeTime = 0;
Timer timer;
public static Camera camera;
private ShakeDetector mShakeDetector;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
Vibrator vibrator;
SurfaceView surfaceView;
SurfaceHolder holder;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flash);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    text = (TextView) findViewById(R.id.text);
    image = (ImageView) findViewById(R.id.flashon);
    // ShakeDetector initialization
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mShakeDetector = new ShakeDetector();
    count = 0;
    image.setVisibility(View.INVISIBLE);
    text.setVisibility(View.VISIBLE);
    surfaceView = (SurfaceView) findViewById(R.id.surface);
    holder = surfaceView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mShakeDetector.setOnShakeListener(new ShakeDetector.OnShakeListener() {

        @Override
        public void onShake(int count) {
            Log.v("elapsed real time", "" + SystemClock.elapsedRealtime());
            Log.v("lastShakeTime", "" + lastShakeTime);
            if (SystemClock.elapsedRealtime() - lastShakeTime < 800) {

            } else {
                handleShakeEvent();
            }
            lastShakeTime = SystemClock.elapsedRealtime();
        }
    });

}

public void handleShakeEvent() {
    try {

        if (getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA_FLASH)) {
            if (camera == null) {
                camera = Camera.open();
                android.hardware.Camera.Parameters p = camera.getParameters();
                p.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                camera.startPreview();
                image.setVisibility(View.VISIBLE);
                text.setVisibility(View.INVISIBLE);
                ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(100);

            } else if (camera != null) {
                android.hardware.Camera.Parameters p = camera.getParameters();
                p.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();
                camera.release();
                camera = null;
                image.setVisibility(View.INVISIBLE);
                text.setVisibility(View.VISIBLE);
                ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(100);
            }
        } else {
            Toast.makeText(getApplicationContext(), "You do not have a flash!", Toast.LENGTH_LONG).show();
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(),
                "Exception", Toast.LENGTH_SHORT).show();
    }

}

@Override
public void onResume() {
    super.onResume();
    mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    mSensorManager.unregisterListener(mShakeDetector);
    finish();

}


@Override
protected void onPause() {
    super.onPause();
    mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    Log.v("in", "created");

 mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
    if (camera == null) {
        try {
            camera = Camera.open();
        } catch (RuntimeException e) {
            System.err.println(e);
            return;
        }
        Camera.Parameters param;
        param = camera.getParameters();
        param.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH);
        image.setVisibility(View.VISIBLE);
        text.setVisibility(View.INVISIBLE);
        ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(100);
        camera.setParameters(param);
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();

        } catch (Exception e) {
            System.err.println(e);
            return;
        }
    }
    else if(camera!=null){
        Camera.Parameters param;
        param=camera.getParameters();
        param.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        image.setVisibility(View.INVISIBLE);
        text.setVisibility(View.VISIBLE);
        ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(100);
        camera.setParameters(param);
        camera.stopPreview();

        camera.release();
        camera=null;
    }

}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    Log.v("in", "changed");


}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    Log.v("in", "destroyed");
    mSensorManager.unregisterListener(mShakeDetector);
    camera.stopPreview();
    camera.release();
    camera = null;
}
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

经过几个小时的努力,我自己解决了这个问题。 :d

我在onShakeListener中调用了方法surfaceChanged(),并将handleShakeEvent()移动到了surfaceChanged()。