23

使用可能なセンサーのリストを表示しようとしていますが、ないようです。
エミュレータのせいだと思っていたのですが、電話で試してみましたが結果は同じでした。

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);
    }
}
4

4 に答える 4

28

定数SensorManager.SENSOR_ALLは非推奨であり、もう機能していないようです。

代わりにセンサーリストをクエリするSensor.TYPE_ALLと、機能するはずです(私のエミュレーターは「Goldfish 3-axis Accelerometer」を返します)。

于 2010-05-18T16:39:20.287 に答える
9

このコードを試すことができます:-

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 レイアウトにリスト ビューを配置するだけです。

于 2014-12-29T15:35:58.093 に答える