2012-12-05 56 views
1
public class MainActivity extends Activity implements SensorEventListener { 
    final String tag = "myLogs"; 
    SensorManager sm = null; 
    Sensor lightSensor; 
    float lightQuantity; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     sm = (SensorManager) getSystemService(SENSOR_SERVICE); 
     lightSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT); 

     if(lightSensor == null) 
      Log.d(tag, "no sensor:("); 
     else 
      Log.d(tag, "GOT IT!"); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     sm.registerListener((SensorEventListener)this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); 
    } 

    @Override 
    protected void onStop() { 
     sm.unregisterListener((SensorEventListener)this); 
     super.onStop(); 
    } 

    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     Log.d(tag,"onAccuracyChanged: " + sensor.getType() + ", accuracy: " + accuracy); 

    } 

    public void onSensorChanged(SensorEvent event) { 
     lightQuantity = event.values[0]; 
     Log.d(tag,"onSensorChanged: " + event.sensor.getType() + ", result: " + lightQuantity); 

    } 
} 

我無法使用此代碼獲取光線傳感器統計信息。 。 「無傳感器:(」消息總是 getSensorList()方法不顯示光傳感器或者 我的設備:的Xperia PRO(mk16i)SE光傳感器的工作原理試驗良好Xperia光線傳感器[Xperia PRO]

回答

0

的是,沒有按的Xperia」的問題。 t通過驅動程序以標準Android方式實現傳感器 只有通過讀取內核變量才能使用的專有實現,如下所述:(c)