Originally to write a detection of the temperature of the mobile phone application, learning about the sensor API, can result how to write No. After detection, found that the sensor was null, it is clear that my phone does not have a type_ambient_temperature sensor.
So I wrote an app that lists all the available sensors. The code is as follows:
1 Packagekempff.sensors;2 3 Importjava.util.List;4 5 Importandroid.app.Activity;6 ImportAndroid.hardware.Sensor;7 ImportAndroid.hardware.SensorManager;8 ImportAndroid.os.Bundle;9 ImportAndroid.widget.TextView;Ten One Public classMainactivityextendsActivity { A PrivateString mtext= ""; - PrivateTextView tv=NULL; - PrivateSensormanager sm=NULL; the - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -tv=(TextView) Findviewbyid (r.id.msg); +Sm= (Sensormanager) This. Getsystemservice (sensor_service); AList<sensor> sensors=sm.getsensorlist (sensor.type_all); at for(Sensor t:sensors) { -Log ("[Sensors]name:" +t.getname () + "; Vendor: "+T.getvendor ()); - } - } - - Private voidlog (String s) { inMtext+=s+ "\ n"; - Tv.settext (mtext); to } +}
After compiling the installation on the phone, the output is not ambient_temperature. On my mobile phone there is a temperature measurement software can detect temperatures, may be used CPU temperature and other methods, have not figured out. Attach another section of the code that uses the temperature sensor to measure temperature.
1 Packagekempff.t001;2 3 ImportAndroid.os.Bundle;4 ImportAndroid.widget.TextView;5 Importandroid.app.Activity;6 ImportAndroid.hardware.*;7 8 9 Public classMainactivityextendsActivityImplementsSensoreventlistener {Ten PrivateSensor mtmp=NULL; One PrivateSensormanager msm=NULL; A PrivateTextView msg=NULL; - PrivateString mtext= ""; - the @Override - protected voidonCreate (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); A if(mtmp==NULL){ atLog ("Type_ambient_temperature==null"); -}Else{ -Log ("Type_ambient_temperature:" +mtmp.tostring ()); - } - } - in @Override - protected voidOnPause () { to //TODO auto-generated Method Stub + Super. OnPause (); -Msm.unregisterlistener ( This); the } * $ @OverridePanax Notoginseng protected voidOnresume () { - //TODO auto-generated Method Stub the Super. Onresume (); +Msm.registerlistener ( This, Mtmp, sensormanager.sensor_delay_normal); A } the + @Override - Public voidOnaccuracychanged (Sensor arg0,intarg1) { $ //TODO auto-generated Method Stub $ - } - the @Override - Public voidonsensorchanged (Sensorevent event) {Wuyi //TODO auto-generated Method Stub the for(floatt:event.values) { -Log (string.valueof (t) + "\ n"); Wu } - } About $ Private voidlog (String txt) { -mtext+=txt; - This. Msg.settext (mtext); - } A}
This is layout.xml.
1 <Relativelayoutxmlns: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" >Ten One <TextView A Android:id= "@+id/msg" - Android:layout_width= "Fill_parent" - Android:layout_height= "Fill_parent" the Android:text= "@string/hello_world" /> - - </Relativelayout>
Get all available sensors on a device on the Android platform