download: http://www.mediafire.com/download/83nnqx9cnhw5nsv/AndroidProximitySensor_110920a.zip
another
-------------------------------------------
another
-------------------------------------------
01 | import android.app.Activity; |
02 | import android.hardware.Sensor; |
03 | import android.hardware.SensorEvent; |
04 | import android.hardware.SensorEventListener; |
05 | import android.hardware.SensorManager; |
06 | import android.os.Bundle; |
07 | import android.widget.ImageView; |
08 |
09 | public class SensorActivity extends Activity implements SensorEventListener { |
10 | private SensorManager mSensorManager; |
11 | private Sensor mSensor; |
12 | ImageView iv; |
13 |
14 | @Override |
15 | protected void onCreate(Bundle savedInstanceState) { |
16 | // TODO Auto-generated method stub |
17 | super.onCreate(savedInstanceState); |
18 | setContentView(R.layout.sensor_screen); |
19 | mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); |
20 | mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); |
21 | iv = (ImageView) findViewById(R.id.imageView1); |
22 | } |
23 |
24 | protected void onResume() { |
25 | super.onResume(); |
26 | mSensorManager.registerListener(this, mSensor, |
27 | SensorManager.SENSOR_DELAY_NORMAL); |
28 | } |
29 |
30 | protected void onPause() { |
31 | super.onPause(); |
32 | mSensorManager.unregisterListener(this); |
33 | } |
34 |
35 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
36 | } |
37 |
38 | public void onSensorChanged(SensorEvent event) { |
39 | if (event.values[0] == 0) { |
40 | iv.setImageResource(R.drawable.near); |
41 | } else { |
42 | iv.setImageResource(R.drawable.far); |
43 | } |
44 | } |
45 | } |
No comments:
Post a Comment