huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.DeviceTest;
 
import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
 
import java.util.List;
 
import com.DeviceTest.helper.ControlButtonUtil;
 
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
 
/**
 * @author LanBinYuan
 * @date 2011-06-11
 * 
 */
 
public class LightsensorTestActivity extends Activity
        implements SensorEventListener {
    private static final String TAG = "LightsensorTestActivity";
 
    private TextView txt_Light;
    private TextView txt_sensor_info;
    private TextView txt_sensor_event;
 
   /** Called when the activity is first created. */
   private SensorManager sensorManager;
   
   protected void onCreate(Bundle paramBundle) {
       super.onCreate(paramBundle);
 
       setTitle(getTitle() + "----("
               + getIntent().getStringExtra(DeviceTest.EXTRA_TEST_PROGRESS) + ")");
       //requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().addFlags(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
       
       setContentView(R.layout.lightsensortest);
        initView();
       ControlButtonUtil.initControlButtonView(this);
       sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
 
   }
 
    private void initView() {
        txt_Light = (TextView) findViewById(R.id.Light);
        txt_Light.setTextColor(android.graphics.Color.RED);
        txt_sensor_info = (TextView) findViewById(R.id.txt_sensor_info);
        txt_sensor_event = (TextView) findViewById(R.id.txt_sensor_event);
        txt_sensor_event.setTextColor(android.graphics.Color.GREEN);
    }
 
   protected void onResume() {
       super.onResume();
 
        List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_LIGHT);
        if (null != sensors) {
            for (Sensor s : sensors) {
                Log.v(TAG, "registerListener " + s.getName());
                //updateSensorInfoText(s);
                sensorManager.registerListener(this, s,
                        SensorManager.SENSOR_DELAY_NORMAL);
                break;
            }
        }
   }
 
   //
   
   protected void onStop() {
       super.onStop();
        Log.v(TAG, "unregisterListener");
        sensorManager.unregisterListener(this);
   }
 
   public boolean dispatchKeyEvent(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           return false;
       }
       return super.dispatchKeyEvent(event);
   }
 
    private void updateSensorInfoText(Sensor sensor) {
        StringBuilder sb = new StringBuilder();
        sb.append("name:" + sensor.getName() + "\n");
        sb.append("vendor:" + sensor.getVendor() + "\n");
        sb.append("version:" + sensor.getVersion() + "\n");
        sb.append("maxRange:" + sensor.getMaximumRange() + "\n");
        sb.append("resolution:" + sensor.getResolution() + "\n");
        sb.append("power:" + sensor.getPower() + "\n");
        txt_sensor_info.setText(sb.toString());
    }
 
    @Override
    public void onSensorChanged(SensorEvent e) {
        Sensor sensor = e.sensor;
        switch (sensor.getType()) {
            case Sensor.TYPE_LIGHT: {
                updateSensorInfoText(sensor);
                String event = "Light:" + e.values[SensorManager.DATA_X];
                //Log.v(TAG, "onSensorChanged" + event);
                txt_sensor_event.setText(event);
            }
            default:
                break;
        }
    }
 
    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
    }
}