private boolean isFlashSupported(PackageManager packageManager) {
return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
private boolean isCameraSupported(PackageManager packageManager) {
return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
public void setTorchState(boolean torchState) {
Log.v(LOG_TAG, "setTorchState - " + torchState);
PackageManager packageManager = getPackageManager();
if (isCameraSupported(packageManager) && isFlashSupported(packageManager)) {
if (camera == null)
try {
camera = Camera.open();
} catch (Exception e) {
e.printStackTrace();
flashOn = false;
}
if (torchState) {
if (camera != null) {
try {
Parameters param = camera.getParameters();
param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(param);
sleep(300);
camera.startPreview();
flashOn = true;
} catch (Exception e) {
e.printStackTrace();
flashOn = false;
}
}
} else {
if (camera != null) {
try {
Parameters param = camera.getParameters();
param.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(param);
camera.stopPreview();
camera.release();
camera = null;
flashOn = false;
} catch (Exception e) {
e.printStackTrace();
}
}
}
sendBroadcast(new Intent(FLASHLIGHT_CHANGED)); // receive in custom receiver
}
}
Thursday, November 23, 2017
Android - Turn flashlight on and off
Android - System settings observers
Observe auto-rotation changes
/**
* Observer to watch for changes to the Auto Rotation setting
*/
private static class RotationObserver extends ContentObserver {
private Context mContext;
RotationObserver(Handler handler, Context context) {
super(handler);
mContext = context;
}
void startObserving() {
ContentResolver resolver = mContext.getContentResolver();
// Listen to accelerometer
resolver.registerContentObserver(Settings.System.getUriFor
(Settings.System.ACCELEROMETER_ROTATION),
true, this);
}
void stopObserving() {
mContext.getContentResolver().unregisterContentObserver(this);
}
@Override
public void onChange(boolean selfChange) {
Intent intent = new Intent(AUTO_ROTATE_CHANGED);
mContext.sendBroadcast(intent); // receive in custom receiver
}
}
Observe brightness changes
/**
* Observer to watch for changes to the BRIGHTNESS setting
*/
private static class BrightnessObserver extends ContentObserver {
private Context mContext;
BrightnessObserver(Handler handler, Context context) {
super(handler);
mContext = context;
}
void startObserving() {
ContentResolver resolver = mContext.getContentResolver();
// Listen to brightness and brightness mode
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.SCREEN_BRIGHTNESS), false, this);
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE), false, this);
}
void stopObserving() {
mContext.getContentResolver().unregisterContentObserver(this);
}
@Override
public void onChange(boolean selfChange) {
Intent intent = new Intent(BRIGHTNESS_CHANGED);
mContext.sendBroadcast(intent); // receive in custom receiver
}
}
Use in Service
public class CustomService extends Service {
...
private BrightnessObserver mBrightnessObserver ;
private RotationObserver mRotationObserver;
...
@Override
public void onCreate() {
super.onCreate();
...
if (mSettingsObserver == null) {
mSettingsObserver = new BrightnessObserver(new Handler(), this);
mSettingsObserver.startObserving();
}
if (mRotationObserver == null) {
mRotationObserver = new RotationObserver(new Handler(), this);
mRotationObserver.startObserving();
}
}
@Override
public void onDestroy() {
super.onDestroy();
...
try {
if (mSettingsObserver != null) {
mSettingsObserver.stopObserving();
mSettingsObserver = null;
}
if (mRotationObserver != null) {
mRotationObserver.stopObserving();
mRotationObserver = null;
}
} catch (Exception e) {
Log.e(LOG_TAG, "", e);
}
}
}
Subscribe to:
Posts (Atom)