Created
May 29, 2014 15:12
-
-
Save goodrahstar/20342aaadab4ea835c24 to your computer and use it in GitHub Desktop.
testing proxi sensor with volume up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.exercise.AndroidProximitySensor; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.hardware.Sensor; | |
| import android.hardware.SensorEvent; | |
| import android.hardware.SensorEventListener; | |
| import android.hardware.SensorManager; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.view.KeyEvent; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| public class AndroidProximitySensorActivity extends Activity { | |
| /** Called when the activity is first created. */ | |
| TextView ProximitySensor, ProximityMax, ProximityReading; | |
| SensorManager mySensorManager; | |
| Sensor myProximitySensor; | |
| final float alpha = (float) 0.8; | |
| float gravity[] = new float[3]; | |
| float linear_acceleration[] = new float[3]; | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.main); | |
| ProximitySensor = (TextView) findViewById(R.id.proximitySensor); | |
| ProximityMax = (TextView) findViewById(R.id.proximityMax); | |
| ProximityReading = (TextView) findViewById(R.id.proximityReading); | |
| mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); | |
| myProximitySensor = mySensorManager | |
| .getDefaultSensor(Sensor.TYPE_PROXIMITY); | |
| if (myProximitySensor == null) { | |
| ProximitySensor.setText("No Proximity Sensor!"); | |
| } else { | |
| ProximitySensor.setText(myProximitySensor.getName()); | |
| ProximityMax.setText("Maximum Range: " | |
| + String.valueOf(myProximitySensor.getMaximumRange())); | |
| //mySensorManager.registerListener(proximitySensorEventListener,myProximitySensor,SensorManager.SENSOR_DELAY_NORMAL); | |
| } | |
| } | |
| public int count; | |
| @Override | |
| public boolean onKeyDown(int keycode, KeyEvent e) { | |
| count=0; | |
| if (e.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) { | |
| count++; | |
| // Toast.makeText(getApplicationContext(), | |
| // "In!!! =)", Toast.LENGTH_LONG).show(); | |
| mySensorManager.registerListener(proximitySensorEventListener, | |
| myProximitySensor, | |
| SensorManager.SENSOR_DELAY_NORMAL); | |
| return true; | |
| } | |
| return super.onKeyDown(keycode, e); | |
| } | |
| SensorEventListener proximitySensorEventListener = new SensorEventListener() { | |
| @Override | |
| public void onAccuracyChanged(Sensor sensor, int accuracy) { | |
| } | |
| @Override | |
| public void onSensorChanged(SensorEvent event) { | |
| gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0]; | |
| gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1]; | |
| gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2]; | |
| linear_acceleration[0] = event.values[0] - gravity[0]; | |
| linear_acceleration[1] = event.values[1] - gravity[1]; | |
| linear_acceleration[2] = event.values[2] - gravity[2]; | |
| if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) { | |
| ProximityReading.setText("Proximity Sensor Reading:" | |
| + String.valueOf((int) linear_acceleration[0]) + " " | |
| + String.valueOf((int) linear_acceleration[1]) + " " | |
| + String.valueOf((int) linear_acceleration[2])); | |
| } | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment