12_Sensor简单实例

  列出Android手机所支持的Sensor

12_Sensor简单实例

package com.example.sensorlist;



import java.util.List;



import android.app.Activity;

import android.hardware.Sensor;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.widget.TextView;



public class MainActivity extends Activity {



    private TextView tvSensors;

    private SensorManager sensorManager;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        tvSensors = (TextView) findViewById(R.id.tvSensors);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        

        List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);

        for(Sensor sensor: sensors){

            tvSensors.append(sensor.getName() + "\n");

        }



    }



}
MainActivity

  使用MotionSensor的简单实例。

12_Sensor简单实例

 1 package com.example.motionsensor;

 2 

 3 import android.app.Activity;

 4 import android.hardware.Sensor;

 5 import android.hardware.SensorEvent;

 6 import android.hardware.SensorEventListener;

 7 import android.hardware.SensorManager;

 8 import android.os.Bundle;

 9 import android.widget.TextView;

10 

11 //为了获取传感器回传的数据,需要implements SensorEventListener

12 public class MainActivity extends Activity implements SensorEventListener {

13 

14     private TextView tvAccelermeter;

15     private SensorManager sensorManager;

16 

17     @Override

18     protected void onCreate(Bundle savedInstanceState) {

19         super.onCreate(savedInstanceState);

20         setContentView(R.layout.activity_main);

21 

22         tvAccelermeter = (TextView) findViewById(R.id.tvAccelermeter);

23 

24         sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

25 

26     }

27 

28     @Override

29     public void onSensorChanged(SensorEvent event) {

30         // 多个传感器,要进行判断

31         switch (event.sensor.getType()) {

32         case Sensor.TYPE_ACCELEROMETER:

33             tvAccelermeter.setText(String.format("%f %f %f", event.values[0],

34                     event.values[1], event.values[2]));

35             break;

36 

37         case Sensor.TYPE_GRAVITY:

38             // tvAccelermeter.setText(String.format("%f %f %f", event.values[0],

39             // event.values[1], event.values[2]));

40             break;

41 

42         default:

43             break;

44         }

45 

46     }

47 

48     @Override

49     public void onAccuracyChanged(Sensor sensor, int accuracy) {

50 

51     }

52 

53     @Override

54     protected void onResume() {

55         super.onResume();

56 

57         // 注册传感器

58         sensorManager.registerListener(this,

59                 sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),

60                 SensorManager.SENSOR_DELAY_UI);

61         sensorManager.registerListener(this,

62                 sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),

63                 SensorManager.SENSOR_DELAY_FASTEST);

64     }

65 

66     @Override

67     protected void onPause() {

68         super.onPause();

69 

70         sensorManager.unregisterListener(this);

71     }

72 

73 }
MainActivity

你可能感兴趣的:(sensor)