I want to get the brightness level from the Android camera.
I need to display the light level detected by the Android camera, is there any posibility to do that ?
private final SensorManager mSensorManager;
private final Sensor mLightSensor;
private float mLightQuantity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Obtain references to the SensorManager and the Light Sensor
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
// Implement a listener to receive updates
SensorEventListener listener = new SensorEventListener() {
#Override
public void onSensorChanged(SensorEvent event) {
lightQuantity = event.values[0];
}
}
// Register the listener with the light sensor -- choosing
// one of the SensorManager.SENSOR_DELAY_* constants.
sensorManager.registerListener(
listener, lightSensor, SensorManager.SENSOR_DELAY_UI);
}
Related
I have created a simple app that uses the accelerometer sensor to calculate the outside force then start a mp3 file. It seems to work well unless I turn screen off (or it automaticlly turns itself off) then it stops completely. How can i fix it. My code here, Thanks.
private MediaPlayer mediaPlayer;
private TextView textView;
private SensorManager sensorManager;
private Sensor accelerometerSensor;
private float maxf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) this.findViewById(R.id.textView);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_UI);
}
#Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){
return;
}
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
float f = Math.round(Math.abs(x*x + y*y + z*z));
if (f>maxf ){ maxf =f;}
StringBuilder sb = new StringBuilder();
sb.append("forrce:").append(f).append("\n");
sb.append("MaxForce:").append(maxf).append("\n");
textView.setText(sb.toString());
if (maxf>150) {
mediaPlayer = MediaPlayer.create(this, R.raw.b);
mediaPlayer.start();
maxf=1;
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
Add on-lock services
[Reference][1]https://developer.android.com/guide/components/services.html
And use accelerometer in that particular activity...Hope it helps :)
I am using AccessibilityService to monitor whenever the foreground activity is changed. Additionally whenever a such a change occurs, I need to read the accelerometer readings for the next 5 seconds. I implemented the following code, but I am not getting continuous readings. Sometimes No reading is shown in between 2 app switches
I am able to detect the foreground activity change accurately, and I can run an activity independently that can show the sensor state changes. But problem exists in combining both.
public class WindowChangeDetectingService extends AccessibilityService implements SensorEventListener {
#Override
protected void onServiceConnected() {
...
}
SensorManager mSensorManager;
Sensor mAccelerometerSensor;
SensorEventListener mListener;
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
try {
mSensorManager.unregisterListener(mListener);
}
catch (Exception e){
Log.d("not unregs",e.toString());
}
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
// do stuff with sensor values
mSensorManager.unregisterListener(mListener);
}
}, 5000);
ComponentName componentName = new ComponentName(
event.getPackageName().toString(),
event.getClassName().toString()
);
ActivityInfo activityInfo = tryGetActivity(componentName);
boolean isActivity = activityInfo != null;
if (isActivity)
Log.i("CurrentActivity", componentName.flattenToShortString());
}
}
#Override
public void onInterrupt() {}
#Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
Log.i("\nAccelerometer: \n","X:"+x+"\tY:"+y+"\tZ:"+z);
}
}
Use SENSOR_DELAY_GAME or SENSOR_DELAY_FASTEST instead of SENSOR_DELAY_NORMAL
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this,
mAccelerometerSensor, SensorManager.SENSOR_DELAY_GAME);//change here
If the user turns over the phone I want to react to that by stopping my text to speech reading. It would be a nice feature for my app, but how can I detect this motion? I'm not really familiar with motion sensors and I could not find this specific motion listener anywhere, mostly just screen orientations. Thanks for the help!
This sample activity demonstrates how a device's gravity sensor can be used to detect when the device is turned over. In method onSensorChanged(), term factor determines how complete the "turn over" must be. Typical range might be 0.7 to 0.95.
Support for Gravity Sensor was added in Android API 9. Not all devices have a gravity sensor.
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private static final String TAG = "Demo";
private SensorManager mSensorManager;
private Sensor mGravitySensor;
private boolean mFacingDown;
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// nothing
}
#Override
public void onSensorChanged(SensorEvent event) {
final float factor = 0.95F;
if (event.sensor == mGravitySensor) {
boolean nowDown = event.values[2] < -SensorManager.GRAVITY_EARTH * factor;
if (nowDown != mFacingDown) {
if (nowDown) {
Log.i(TAG, "DOWN");
} else {
Log.i(TAG, "UP");
}
mFacingDown = nowDown;
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mGravitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
if (mGravitySensor == null) {
Log.w(TAG, "Device has no gravity sensor");
}
}
#Override
protected void onResume() {
super.onResume();
if (mGravitySensor != null) {
mSensorManager.registerListener(this, mGravitySensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
#Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
I know that there are only two values of proximity : 0.0 & 1.0
I need a simple code to detect proximity through the device's sensors, and perform any task if proximity changes to 1.0
this is what i found.
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sensor_screen);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
iv = (ImageView) findViewById(R.id.imageView1);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
if (event.values[0] == 0) {
iv.setImageResource(R.drawable.near);
} else {
iv.setImageResource(R.drawable.far);
}
}
}
You can use the getMaximumRange() method to obtain your proximity sensor's max range and use it to determine what to do.
SensorManager sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
float maxRange = proximitySensor.getMaximumRange();
Now use maxRange to do what you want to do.
public void onSensorChanged(SensorEvent event) {
if(maxRange == event.values[0]) {
// Do something when something is far away.
}
else {
// Do something when something is near.
}
}
One very important thing. The proximity sensor varies from device to device. It not always reports 1.0 and 0.0. For example my Moto G (1st Gen.) reports 3 cm as its min range and 100 cm as its max range. So its best to obtain the max range of the device's sensor and use it in your code rather then hard-coding the value.
I'm trying to get both accelerometer and orientation data. Currently, I created an AccelerometerManager and OrientationManager. They both do the same thing; implement SensorEventListener and retrieve data from values[] in onSensorChanged() listeners.
Is there an easier way to do this? It seems like having 2 handlers with duplicate code is uneccessary. Is there a way to access a values[] array with the combined accelerometer and orientation data together?
Because of the 2 handlers, I'm also having to do this:
orientationManager = new OrientationManager(this);
orientationSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
orientationSensorManager.registerListener(orientationManager,
orientationSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
accelerometerManager = new AccelerometerManager(this);
accelerometerSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometerSensorManager.registerListener(accelerometerManager,
orientationSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
Can you try something like this:
private SensorManager mSensorManager;
private SensorEventListener mSensorListener;
////
mSensorManager = (SensorManager) this
.getSystemService(Context.SENSOR_SERVICE);
mSensorListener = new SensorEventListener() {
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
#Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
//TODO: get values
}else if (sensor.getType() == Sensor.TYPE_ORIENTATION) {
//TODO: get values
}
}
}
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);