我正在尝试显示可用传感器的列表,但似乎没有!
我认为这是因为模拟器,但我尝试了在手机上,结果是一样的。
private SensorManager mSensorManager;
TextView mSensorsTot,mSensorAvailables;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the texts fields of the layout and setup to invisible
mSensorsTot = (TextView) findViewById(R.id.sensoritot);
mSensorAvailables = (TextView) findViewById(R.id.sensoridisponibili);
// Get the SensorManager
mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
// List of Sensors Available
List<Sensor> msensorList = mSensorManager.getSensorList(SensorManager.SENSOR_ALL);
// Print how may Sensors are there
mSensorsTot.setText(msensorList.size()+" "+this.getString(R.string.sensors)+"!");
// Print each Sensor available using sSensList as the String to be printed
String sSensList = new String("");
Sensor tmp;
int x,i;
for (i=0;i<msensorList.size();i++){
tmp = msensorList.get(i);
sSensList = " "+sSensList+tmp.getName(); // Add the sensor name to the string of sensors available
}
// if there are sensors available show the list
if (i>0){
sSensList = getString(R.string.sensors)+":"+sSensList;
mSensorAvailables.setText(sSensList);
}
}
发布于 2010-05-19 00:39:20
常量SensorManager.SENSOR_ALL
已被弃用,似乎不再起作用。
改为使用Sensor.TYPE_ALL
查询传感器列表,它应该可以工作(我的仿真器返回"Goldfish 3轴加速度计“)。
发布于 2014-12-29 23:35:58
你可以试试这段代码:
package com.example.sensor;
import java.util.List;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
SensorManager smm;
List<Sensor> sensor;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
lv = (ListView) findViewById (R.id.listView1);
sensor = smm.getSensorList(Sensor.TYPE_ALL);
lv.setAdapter(new ArrayAdapter<Sensor>(this, android.R.layout.simple_list_item_1, sensor));
}
}
只需在xml布局中放置一个列表视图即可。
发布于 2017-03-21 23:12:40
您可以尝试按如下方式列出传感器:
String cAPP_FOLDER =文件夹的路径
String cSENSOR_FILE =文件的名称
// Create a file to export the sensor info.
File fp = new File(cAPP_FOLDER + cSENSOR_FILE);
fp.createNewFile();
PrintWriter pw = new PrintWriter(new FileWriter(fp, true));
SensorManager oSM = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensorsList = oSM.getSensorList(Sensor.TYPE_ALL);
for (Sensor s : sensorsList) {
pw.write(s.toString() + "\n");
}
pw.close();
//////////////////////////////////////////////////////////////////////////////
//输出文件
{传感器name=“博世加速度计传感器",vendor=”博世传感器GmbH",version=2,type=1,maxRange=156.88,分辨率=5.984497E-4,power=0.13,minDelay=10000}
{传感器name=“博世磁场传感器”,vendor=“博世传感器GmbH",version=2,type=2,maxRange=1600.0,resolution=0.3,power=0.5,minDelay=40000}
{传感器name=“博世方向传感器”,vendor=“博世传感器GmbH",version=2,type=3,maxRange=360.0,resolution=1.0,power=0.63,minDelay=5000}
{传感器name=“博世陀螺仪传感器”,vendor=“博世传感器GmbH",version=2,type=4,maxRange=2500.0,resolution=0.0038146973,power=5.0,minDelay=5000}
{传感器name=“博世重力传感器”,vendor=“博世传感器GmbH",version=2,type=9,maxRange=19.61,分辨率=5.984497E-4,power=5.63,minDelay=5000}
{传感器name=“博世线性加速度传感器”,vendor=“博世传感器GmbH",version=2,type=10,maxRange=19.61,分辨率=5.984497E-4,power=5.63,minDelay=5000}
{传感器name=“博世旋转矢量传感器”,vendor=“博世传感器GmbH",version=2,type=11,maxRange=1.0,分辨率=5.9604645E-8,power=5.63,minDelay=5000}
{传感器name=“博世磁场未校准传感器”,vendor=“博世传感器GmbH",version=2,type=14,maxRange=1600.0,resolution=0.3,power=0.5,minDelay=40000}
{传感器name=“博世游戏旋转矢量传感器”,vendor=“博世传感器GmbH",version=2,type=15,maxRange=1.0,分辨率=5.9604645E-8,power=5.63,minDelay=5000}
{传感器name=“博世陀螺仪未校准传感器”,vendor=“博世传感器GmbH",version=2,type=16,maxRange=2500.0,resolution=0.0038146973,power=5.0,minDelay=5000}
{传感器name=“博世地磁旋转矢量传感器”,vendor=“博世传感器GmbH",version=2,type=20,maxRange=1.0,分辨率=5.9604645E-8,power=5.63,minDelay=5000}
{传感器name="LIGHT",vendor="MTK",version=1,type=5,maxRange=10240.0,resolution=1.0,power=0.13,minDelay=0}
{传感器name=“接近”,vendor="MTK",version=1,type=8,maxRange=1.0,resolution=1.0,power=0.13,minDelay=0}
{传感器name="SIGNIFICANT_MOTION",vendor="MTK",version=1,type=17,maxRange=85.0,resolution=0.1,power=0.5,minDelay=-1}
https://stackoverflow.com/questions/2858161
复制相似问题