Android光線傳感器使用方法詳解
更新時間:2022年09月20日 08:56:12 作者:路宇
這篇文章主要為大家詳細介紹了Android光線傳感器的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android光線傳感器使用的具體代碼,供大家參考,具體內容如下
一、首先是布局頁面activity_light_sensor.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context=".LightSensorActivity"> ? ? <TextView ? ? ? ? android:id="@+id/textView" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="60dp" ? ? ? ? android:gravity="center" ? ? ? ? android:text="光線傳感器" ? ? ? ? android:textColor="@color/black" ? ? ? ? android:textSize="20sp" /> ? ? <EditText ? ? ? ? android:id="@+id/editText" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" /> </LinearLayout>
二、在對應的Activity中獲取光線傳感器的值LightSensorActivity,具體注釋已經在代碼中給出
public class LightSensorActivity extends AppCompatActivity implements SensorEventListener { ? ? private EditText editText; ? ? //傳感器管理器對象 ? ? private SensorManager sensorManager; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_light_sensor); ? ? ? ? editText = findViewById(R.id.editText); ? ? ? ? sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); ? ? } ? ? @Override ? ? protected void onResume() { ? ? ? ? super.onResume(); ? ? ? ? //第一個參數:SensorEventListener對象用this來指定就可以了 ? ? ? ? // 第二個參數:傳感器對象 光線傳感器類型的常量:TYPE_LIGHT ? ? ? ? // 第三個參數:傳感器數據的頻率 這里采用適合游戲的頻率 ? ? ? ? sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_GAME); ? ? } ? ? @Override ? ? protected void onPause() { ? ? ? ? super.onPause(); ? ? ? ? sensorManager.unregisterListener(this); ? ? } ? ? //當傳感器的值,發(fā)生變化時,回調的方法 ? ? @Override ? ? public void onSensorChanged(SensorEvent event) { ? ? ? ? //獲取傳感器的值 ? ? ? ? float[] values= event.values; ? ? ? ? //獲取傳感器類型 ? ? ? ? int sensorType = event.sensor.getType(); ? ? ? ? StringBuilder stringBuilder = null; ? ? ? ? if (sensorType==Sensor.TYPE_LIGHT){ ? ? ? ? ? ? stringBuilder = new StringBuilder(); ? ? ? ? ? ? stringBuilder.append("光的強度值:"); ? ? ? ? ? ? //添加獲取的傳感器的值 ? ? ? ? ? ? stringBuilder.append(values[0]); ? ? ? ? ? ? editText.setText(stringBuilder.toString()); ? ? ? ? } ? ? } ? ? //當傳感器的精度,發(fā)生變化時,回調的方法 ? ? @Override ? ? public void onAccuracyChanged(Sensor sensor, int accuracy) { ? ? } }
效果如圖所示:
以上是光線傳感器的簡單使用。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android?Activity通用懸浮可拖拽View封裝的思路詳解
這篇文章主要介紹了Android?Activity通用懸浮可拖拽View封裝,實現思路是通過封裝通用的基礎懸浮View,繼承通用View,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07Android JSON數據與實體類之間的相互轉化(GSON的用法)
這篇文章主要介紹了Android JSON數據與實體類之間的相互轉化(GSON的用法),非常具有實用價值,需要的朋友可以參考下。2017-01-01Android實現動態(tài)向Gallery中添加圖片及倒影與3D效果示例
這篇文章主要介紹了Android實現動態(tài)向Gallery中添加圖片及倒影與3D效果的方法,涉及Android針對圖片的加載、顯示、翻轉、倒影等相關特效功能實現技巧2016-08-08Android GridView 滑動條設置一直顯示狀態(tài)(推薦)
這篇文章主要介紹了Android GridView 滑動條設置一直顯示狀態(tài)的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12