Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package uwaterloo.ca.lab1_202_14;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import static android.view.View.X;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextView LIGHT = (TextView) findViewById(R.id.LIGHT);
- LIGHT.setText("Initialize");
- LinearLayout rl =(LinearLayout) findViewById(R.id.label2);
- rl.setOrientation(LinearLayout.VERTICAL);
- TextView tv1 = new TextView (getApplicationContext());
- rl.addView(tv1);
- SensorManager senManager =(SensorManager) getSystemService(SENSOR_SERVICE);//init the sensor manager
- //light
- Sensor lightSensor = senManager.getDefaultSensor(Sensor.TYPE_LIGHT); //attaching the light sensor to sensor manager
- LightSensorHandler lightHandler = new LightSensorHandler(LIGHT);
- senManager.registerListener(lightHandler, lightSensor, senManager.SENSOR_DELAY_NORMAL);
- //ACCELOMETER
- TextView ACCELEROMETER=(TextView) findViewById(R.id.X);
- ACCELEROMETER.setText("Inititalize");
- Sensor accelSensor = senManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- XSensorHandler accelerometer =new XSensorHandler(ACCELEROMETER);
- senManager.registerListener(accelerometer,accelSensor,senManager.SENSOR_DELAY_NORMAL);
- }
- class LightSensorHandler implements SensorEventListener {
- TextView tv1;
- public LightSensorHandler(TextView targetTV){
- this.tv1 = targetTV;
- }
- public void onAccuracyChanged(Sensor s, int i){
- }
- //Required by the SensorEventListener interface
- public void onSensorChanged(SensorEvent se){
- if(se.sensor.getType()== Sensor.TYPE_LIGHT){
- tv1.setText(String.valueOf(se.values[0]));
- }
- }
- }
- class XSensorHandler implements SensorEventListener{
- TextView X;
- public XSensorHandler(TextView targetX){
- this.X=targetX;}
- public void onAccuracyChanged(Sensor a,int b){}
- public void onSensorChanged(SensorEvent se){
- if(se.sensor.getType()==Sensor.TYPE_ACCELEROMETER){X.setText((String.valueOf(se.values[0])));}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement