Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. package uwaterloo.ca.lab1_202_14;
  2.  
  3. import android.hardware.Sensor;
  4. import android.hardware.SensorEvent;
  5. import android.hardware.SensorEventListener;
  6. import android.hardware.SensorManager;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.widget.LinearLayout;
  10. import android.widget.TextView;
  11.  
  12. import static android.view.View.X;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. TextView LIGHT = (TextView) findViewById(R.id.LIGHT);
  21. LIGHT.setText("Initialize");
  22.  
  23. LinearLayout rl =(LinearLayout) findViewById(R.id.label2);
  24. rl.setOrientation(LinearLayout.VERTICAL);
  25. TextView tv1 = new TextView (getApplicationContext());
  26.  
  27. rl.addView(tv1);
  28.  
  29. SensorManager senManager =(SensorManager) getSystemService(SENSOR_SERVICE);//init the sensor manager
  30.  
  31. //light
  32. Sensor lightSensor = senManager.getDefaultSensor(Sensor.TYPE_LIGHT); //attaching the light sensor to sensor manager
  33. LightSensorHandler lightHandler = new LightSensorHandler(LIGHT);
  34. senManager.registerListener(lightHandler, lightSensor, senManager.SENSOR_DELAY_NORMAL);
  35.  
  36. //ACCELOMETER
  37.  
  38. TextView ACCELEROMETER=(TextView) findViewById(R.id.X);
  39. ACCELEROMETER.setText("Inititalize");
  40. Sensor accelSensor = senManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  41. XSensorHandler accelerometer =new XSensorHandler(ACCELEROMETER);
  42. senManager.registerListener(accelerometer,accelSensor,senManager.SENSOR_DELAY_NORMAL);
  43.  
  44.  
  45.  
  46.  
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. class LightSensorHandler implements SensorEventListener {
  58.  
  59. TextView tv1;
  60.  
  61. public LightSensorHandler(TextView targetTV){
  62. this.tv1 = targetTV;
  63. }
  64.  
  65. public void onAccuracyChanged(Sensor s, int i){
  66.  
  67. }
  68.  
  69. //Required by the SensorEventListener interface
  70. public void onSensorChanged(SensorEvent se){
  71.  
  72. if(se.sensor.getType()== Sensor.TYPE_LIGHT){
  73. tv1.setText(String.valueOf(se.values[0]));
  74. }
  75.  
  76. }
  77.  
  78.  
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. class XSensorHandler implements SensorEventListener{
  88.  
  89. TextView X;
  90.  
  91.  
  92. public XSensorHandler(TextView targetX){
  93. this.X=targetX;}
  94.  
  95. public void onAccuracyChanged(Sensor a,int b){}
  96.  
  97.  
  98. public void onSensorChanged(SensorEvent se){
  99.  
  100. if(se.sensor.getType()==Sensor.TYPE_ACCELEROMETER){X.setText((String.valueOf(se.values[0])));}
  101.  
  102. }
  103.  
  104. }
  105.  
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement