上代码!
public class MainActivity extends AppCompatActivity { private TextView macc,mlux; private ListView mListView = null; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); macc = (TextView) findViewById(R.id.acc); mlux= (TextView) findViewById(R.id.lux); mListView = (ListView) findViewById(R.id.listview); SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); boolean xx =sensorManager.registerListener(new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { //精度 float acc = event.accuracy; //values 某种传感器的变量程度 float lux = event.values[0]; dialog= new ProgressDialog(MainActivity.this); dialog.setMessage("精度:"+ acc+"光线强度:" +lux); dialog.setCancelable(true); dialog.show(); //mListView.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, (float) acc+(float) lux)); Toast.makeText(MainActivity.this,"精度:"+acc,Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this,"光线强度:"+lux,Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this,"do not speak, Looking!",Toast.LENGTH_LONG).show(); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { //精度发生变化使用该方法 } },lightSensor,SensorManager.SENSOR_DELAY_NORMAL);
//这里的几个是参数注意下
//本人还有一个问题不会 请教 如何让这些数据传递到界面上 } }