一、引言
- 描述:關(guān)于傳感器的使用,我在同欄目下發(fā)了一篇關(guān)于傳感器(方向傳感器、加速度傳感器)的使用,這篇博客主要以獲取不同手機(jī)所支持的傳感器信息為主,具體如何使用這些傳感器,需要自己進(jìn)行查閱和學(xué)習(xí),也可以私聊我。
- 博客:傳感器(方向傳感器、加速度傳感器):http://t.csdn.cn/mLstV
- 難度:初級
- 知識點:Sensors
- 效果
二、了解
1、概述
(首先看一下官網(wǎng)文檔)
(大致意思)
???????大多數(shù) Android 設(shè)備都有內(nèi)置傳感器,可以測量運(yùn)動、方向、 以及各種環(huán)境條件。這些傳感器能夠提供高 精度和準(zhǔn)確度,在要監(jiān)視三維設(shè)備移動或 定位,或者您想要監(jiān)視設(shè)備附近周圍環(huán)境的變化。例如,一個 游戲可能會跟蹤設(shè)備重力傳感器的讀數(shù),以推斷復(fù)雜的用戶手勢 和運(yùn)動,例如傾斜、搖晃、旋轉(zhuǎn)或擺動。同樣,天氣應(yīng)用程序可能會使用 設(shè)備的溫度傳感器和濕度傳感器,用于計算和報告露點或行程 應(yīng)用程序可以使用地磁場傳感器和加速度計來報告指南針 軸承。
Android 平臺支持三大類傳感器:
運(yùn)動傳感器
這些傳感器測量沿三個軸的加速力和旋轉(zhuǎn)力。這 類別包括加速度計、重力傳感器、陀螺儀和旋轉(zhuǎn)矢量 傳感器。
環(huán)境傳感器
這些傳感器測量各種環(huán)境參數(shù),例如環(huán)境空氣溫度 以及壓力、照明和濕度。此類別包括氣壓計、光度計和 溫度計。
位置傳感器
這些傳感器測量設(shè)備的物理位置。此類別包括 方向傳感器和磁力計。
2、關(guān)鍵
如果要遍歷所有傳感器,那么這個就是關(guān)鍵
文章來源:http://www.zghlxwxcb.cn/news/detail-495170.html
List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
三、設(shè)計
如果只是遍歷傳感器信息,一個BaseAdapter適配器即可文章來源地址http://www.zghlxwxcb.cn/news/detail-495170.html
1、UI設(shè)計
(1)主界面
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/bin_sensor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="獲取當(dāng)前設(shè)備支持的傳感器"/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
(2)適配器item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="75dp"
android:textSize="18dp"/>
</LinearLayout>
2、編碼
(1)獲取數(shù)據(jù)(傳感器信息)
private Button sensor;
private SensorManager sm;
private ListView list;
private String[] adapterData;
private void init() {
list = findViewById(R.id.list);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = findViewById(R.id.bin_sensor);
sensor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
adapterData = new String[sensors.size()];
for (int i = 0;i < sensors.size();i++) {
adapterData[i] = sensors.get(i).getName();
}
// 初始化適配器
initAdapter();
}
});
}
(2)渲染數(shù)據(jù)(初始化適配器)
static class AppView{
TextView name;
}
private void initAdapter() {
BaseAdapter baseAdapter = new BaseAdapter() {
@Override
public int getCount() {
return adapterData.length;
}
@Override
public Object getItem(int position) {
return adapterData[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppView appView = null;
if (convertView == null) {
convertView = View.inflate(MainActivity.this,R.layout.layout_item,null);
appView = new AppView();
appView.name = convertView.findViewById(R.id.name);
convertView.setTag(appView);
} else {
appView = (AppView) convertView.getTag();
}
appView.name.setText(adapterData[position]);
return convertView;
}
};
list.setAdapter(baseAdapter);
}
到了這里,關(guān)于【Android開發(fā)基礎(chǔ)】手機(jī)傳感器信息的獲取的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!