查看: 467|回复: 0

[Android教程] 详解 android 光线传感器 light sensor的使用

发表于 2017-11-28 08:00:01

调用anroid的光线传感器使用。

实现效果图:

MainActivity.Java

  1. package hk.ust.cse.comp107x.ligthsensor;
  2. import android.content.Context;
  3. import android.hardware.Sensor;
  4. import android.hardware.SensorEvent;
  5. import android.hardware.SensorEventListener;
  6. import android.hardware.SensorManager;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.widget.TextView;
  10. public class MainActivity extends AppCompatActivity implements SensorEventListener{
  11. private SensorManager mSensorManager;
  12. private Sensor mPressure;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. // Get an instance of the sensor service, and use that to get an instance of
  18. // a particular sensor.
  19. mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  20. mPressure = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
  21. }
  22. @Override
  23. public final void onAccuracyChanged(Sensor sensor, int accuracy) {
  24. // Do something here if sensor accuracy changes.
  25. }
  26. @Override
  27. public final void onSensorChanged(SensorEvent event) {
  28. float light = event.values[0];
  29. TextView v = (TextView)findViewById(R.id.textView);
  30. v.setText(Float.toString(light));
  31. // Do something with this sensor data.
  32. }
  33. @Override
  34. protected void onResume() {
  35. // Register a listener for the sensor.
  36. super.onResume();
  37. mSensorManager.registerListener(this, mPressure, SensorManager.SENSOR_DELAY_NORMAL);
  38. }
  39. @Override
  40. protected void onPause() {
  41. // Be sure to unregister the sensor when the activity pauses.
  42. super.onPause();
  43. mSensorManager.unregisterListener(this);
  44. }
  45. }
复制代码

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="hk.ust.cse.comp107x.ligthsensor.MainActivity">
  11. <TextView
  12. android:id="@+id/textView"
  13. android:textSize="50dp"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Hello World!"
  17. android:layout_alignParentTop="true"
  18. android:layout_centerHorizontal="true"
  19. android:layout_marginTop="177dp" />
  20. </RelativeLayout>
复制代码

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="hk.ust.cse.comp107x.ligthsensor">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10. <activity android:name=".MainActivity">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. </application>
  17. </manifest>
复制代码

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条