本来要写一个检测手机的温度的小应用,学习一下传感器的api,可结果怎么写不行。经检测,发现取得的Sensor为NULL,这才明白,我手机没有TYPE_AMBIENT_TEMPERATURE传感器。

于是写了一个APP列出所有的可用的Sensors。

(附编译后的APK: http://pan.baidu.com/s/1kToTpTt)

代码如下:

 package kempff.sensors;

 import java.util.List;

 import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView; public class MainActivity extends Activity {
private String mtext="";
private TextView tv=null;
private SensorManager sm=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.msg);
sm=(SensorManager)this.getSystemService(SENSOR_SERVICE);
List<Sensor> sensors=sm.getSensorList(Sensor.TYPE_ALL);
for(Sensor t:sensors){
log("[Sensors]Name:"+t.getName()+";Vendor:"+t.getVendor());
}
} private void log(String s){
mtext+=s+"\n";
tv.setText(mtext);
}
}

编译后在手机上安装,输出结果果然没有AMBIENT_TEMPERATURE。在我手机上有个测温软件竟能检测温度,可能是用的CPU温度之类的方法,还没有弄明白。附上另一段利用温度传感器测温度的代码。

取得Android平台某设备上所有可用的Sensors-LMLPHP

 package kempff.t001;

 import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.hardware.*; public class MainActivity extends Activity implements SensorEventListener {
private Sensor mtmp=null;
private SensorManager msm=null;
private TextView msg=null;
private String mtext=""; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msg=(TextView)findViewById(R.id.msg);
msm=(SensorManager)this.getSystemService(SENSOR_SERVICE);
mtmp=msm.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
if(mtmp==null){
log("TYPE_AMBIENT_TEMPERATURE==NULL");
}else{
log("TYPE_AMBIENT_TEMPERATURE:"+mtmp.toString());
}
} @Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
msm.unregisterListener(this);
} @Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
msm.registerListener(this, mtmp, SensorManager.SENSOR_DELAY_NORMAL);
} @Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub } @Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
for(float t:event.values){
log(String.valueOf(t)+"\n");
}
} private void log(String txt){
mtext+=txt;
this.msg.setText(mtext);
}
}

这是Layout.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kempff.t001.MainActivity" > <TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello_world" /> </RelativeLayout>
04-23 21:09