0
我正在開發一款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;
}
}
在此先感謝。