首页 > 代码库 > 取得Android平台某设备上所有可用的Sensors

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

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

于是写了一个APP列出所有的可用的Sensors。代码如下:

 1 package kempff.sensors; 2  3 import java.util.List; 4  5 import android.app.Activity; 6 import android.hardware.Sensor; 7 import android.hardware.SensorManager; 8 import android.os.Bundle; 9 import android.widget.TextView;10 11 public class MainActivity extends Activity {12     private String mtext="";13     private TextView tv=null;14     private SensorManager sm=null;15 16     @Override17     protected void onCreate(Bundle savedInstanceState) {18         super.onCreate(savedInstanceState);19         setContentView(R.layout.activity_main);20         tv=(TextView)findViewById(R.id.msg);21         sm=(SensorManager)this.getSystemService(SENSOR_SERVICE);22         List<Sensor> sensors=sm.getSensorList(Sensor.TYPE_ALL);23         for(Sensor t:sensors){24             log("[Sensors]Name:"+t.getName()+";Vendor:"+t.getVendor());25         }26     }27     28     private void log(String s){29         mtext+=s+"\n";30         tv.setText(mtext);31     }32 }

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

 1 package kempff.t001; 2  3 import android.os.Bundle; 4 import android.widget.TextView; 5 import android.app.Activity; 6 import android.hardware.*; 7  8  9 public class MainActivity extends Activity implements SensorEventListener {10     private Sensor mtmp=null;11     private SensorManager msm=null;12     private TextView msg=null;13     private String mtext="";14 15     @Override16     protected void onCreate(Bundle savedInstanceState) {17         super.onCreate(savedInstanceState);18         setContentView(R.layout.activity_main);19         msg=(TextView)findViewById(R.id.msg);20         msm=(SensorManager)this.getSystemService(SENSOR_SERVICE);21         mtmp=msm.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);22         if(mtmp==null){23             log("TYPE_AMBIENT_TEMPERATURE==NULL");24         }else{25             log("TYPE_AMBIENT_TEMPERATURE:"+mtmp.toString());26         }27     }28 29     @Override30     protected void onPause() {31         // TODO Auto-generated method stub32         super.onPause();33         msm.unregisterListener(this);34     }35 36     @Override37     protected void onResume() {38         // TODO Auto-generated method stub39         super.onResume();40         msm.registerListener(this, mtmp, SensorManager.SENSOR_DELAY_NORMAL);41     }42 43     @Override44     public void onAccuracyChanged(Sensor arg0, int arg1) {45         // TODO Auto-generated method stub46         47     }48 49     @Override50     public void onSensorChanged(SensorEvent event) {51         // TODO Auto-generated method stub52         for(float t:event.values){53             log(String.valueOf(t)+"\n");54         }        55     }56     57     private void log(String txt){58         mtext+=txt;59         this.msg.setText(mtext);60     }61 }

 

这是Layout.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:paddingBottom="@dimen/activity_vertical_margin" 6     android:paddingLeft="@dimen/activity_horizontal_margin" 7     android:paddingRight="@dimen/activity_horizontal_margin" 8     android:paddingTop="@dimen/activity_vertical_margin" 9     tools:context="kempff.t001.MainActivity" >10 11     <TextView12         android:id="@+id/msg"13         android:layout_width="fill_parent"14         android:layout_height="fill_parent"15         android:text="@string/hello_world" />16 17 </RelativeLayout>

 

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