详解android光线传感器lightsensor的使用-创新互联

调用anroid的光线传感器使用。

创新互联建站主要从事成都做网站、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务康马,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

实现效果图:

详解 android 光线传感器 light sensor的使用

MainActivity.Java


package hk.ust.cse.comp107x.ligthsensor; 
 
import android.content.Context; 
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.TextView; 
 
public class MainActivity extends AppCompatActivity implements SensorEventListener{ 
  private SensorManager mSensorManager; 
  private Sensor mPressure; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // Get an instance of the sensor service, and use that to get an instance of 
    // a particular sensor. 
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    mPressure = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); 
  } 
  @Override 
  public final void onAccuracyChanged(Sensor sensor, int accuracy) { 
    // Do something here if sensor accuracy changes. 
  } 
  @Override 
  public final void onSensorChanged(SensorEvent event) { 
    float light = event.values[0]; 
    TextView v = (TextView)findViewById(R.id.textView); 
    v.setText(Float.toString(light)); 
    // Do something with this sensor data. 
  } 
  @Override 
  protected void onResume() { 
    // Register a listener for the sensor. 
    super.onResume(); 
    mSensorManager.registerListener(this, mPressure, SensorManager.SENSOR_DELAY_NORMAL); 
  } 
 
  @Override 
  protected void onPause() { 
    // Be sure to unregister the sensor when the activity pauses. 
    super.onPause(); 
    mSensorManager.unregisterListener(this); 
  } 
} 

网站名称:详解android光线传感器lightsensor的使用-创新互联
文章URL:http://ybzwz.com/article/jhcsg.html