Android 深入解析光感應器(二)

來源:互聯網
上載者:User

標籤:

光線感應器示範1

       講了一大堆的理論,那麼以下的範例就來展示一下光線感應器的使用.為什麼充分展現光感的用法,我這個範例寫的很easy,僅僅寫了使用光感必須的代碼,然後用了幾個textView將光線變化的值展現到使用者介面。讓使用者能夠隨時的看到光度的變化。




以下是Activity的代碼:


java代碼:

  1. public class LightDemoActivity extends Activity implements SensorEventListener{
  2. /** Called when the activity is first created. */
  3. SensorManager sensorManager = null;//感應器管理器引用
  4. Sensor lightSensor = null;//光線感應器引用

  5. //各個文本的引用
  6. TextView accuracy_view= null;
  7. TextView value_0 = null;
  8. TextView value_1 = null;
  9. TextView value_2 = null;

  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);

  14. //獲得感應器管理器執行個體
  15. sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  16. //獲得光線感應器執行個體
  17. lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
  18. //獲得各個TextView
  19. accuracy_view = (TextView)findViewById(R.id.accuracy);
  20. value_0 = (TextView)findViewById(R.id.value_0);
  21. value_1 = (TextView)findViewById(R.id.value_1);
  22. value_2 = (TextView)findViewById(R.id.value_2);
  23. }

  24. @Override
  25. protected void onPause() {
  26. // TODO Auto-generated method stub
  27. super.onPause();
  28. //登出
  29. sensorManager.unregisterListener(this, lightSensor);
  30. }

  31. @Override
  32. protected void onResume() {
  33. // TODO Auto-generated method stub
  34. super.onResume();
  35. //為感應器管理器注冊監聽
  36. sensorManager.registerListener(this,lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
  37. }

  38. @Override
  39. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  40. // TODO Auto-generated method stub
  41. if(sensor.getType() == Sensor.TYPE_LIGHT){
  42. //設定將accuracy的值顯示到螢幕上
  43. accuracy_view.setText("accuracy:"+accuracy);
  44. }

  45. }

  46. @Override
  47. public void onSensorChanged(SensorEvent event) {
  48. // TODO Auto-generated method stub
  49. if(event.sensor.getType() == Sensor.TYPE_LIGHT){

  50. //將values的值顯示到螢幕上
  51. float[] values = event.values;
  52. value_0.setText("value[0]:"+values[0]);
  53. value_1.setText("value[1]:"+values[1]);
  54. value_2.setText("value[2]:"+values[2]);
  55. }
  56. }

  57. }
複製代碼
我們來看看解說的代碼:

java代碼:
  1. 以下解說代碼:
  2. //獲得感應器管理器執行個體
  3. sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  4. //獲得光線感應器執行個體
  5. lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

  6. 這段代碼的作用是獲得感應器管理器引用,並獲得光感感應器應用。
  7. protected void onResume() {
  8. // TODO Auto-generated method stub
  9. super.onResume();
  10. //為感應器管理器注冊監聽
  11. sensorManager
  12. .registerListener(this,lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
  13. }
  14. 這段代碼的作用在resume的時候又一次注冊監聽,開啟光感
  15. protected void onPause() {
  16. // TODO Auto-generated method stub
  17. super.onPause();
  18. //登出
  19. sensorManager.unregisterListener(this, lightSensor);
  20. }
  21. 這段代碼的作用是在暫停時登出監聽,關閉光感。由於感應器耗電,所以在不用的時候應該關閉他
  22. if(event.sensor.getType() == Sensor.TYPE_LIGHT)
  23. {
  24. //將values的值顯示到螢幕上
  25. float[] values = event.values;
  26. value_0.setText("value[0]:"+values[0]);
  27. value_1.setText("value[1]:"+values[1]);
  28. value_2.setText("value[2]:"+values[2]);
  29. }

  30. }
複製代碼

Android 深入解析光感應器(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.