1.
2.
1 import android.app.Activity; 2 import android.content.Context; 3 import android.hardware.Sensor; 4 import android.hardware.SensorEvent; 5 import android.hardware.SensorEventListener; 6 import android.hardware.SensorManager; 7 import android.os.Bundle; 8 9 public class MainActivity extends Activity { 10 11 private SensorManager sensorManager; 12 @Override 13 public void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 17 sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 18 Sensor lightSensor = (Sensor)sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); 19 20 sensorManager.registerListener(new SensorEventListener() { 21 22 public void onSensorChanged(SensorEvent event) { 23 String sensorName = event.sensor.getName(); 24 String sensorVendor = event.sensor.getVendor(); 25 float resolution = event.sensor.getResolution(); 26 float power = event.sensor.getPower(); 27 double timestamp = event.timestamp; 28 29 System.out.println("sensor name--->" + sensorName); 30 System.out.println("sensor vendor--->" + sensorVendor); 31 System.out.println("sensor resolution--->" + resolution); 32 System.out.println("sensor power--->" + power); 33 System.out.println("timestamp--->" + timestamp); 34 35 } 36 37 public void onAccuracyChanged(Sensor sensor, int accuracy) { 38 } 39 }, lightSensor, SensorManager.SENSOR_DELAY_GAME); 40 } 41 }