Obtain all available Sensors and androidsensors on a device on the Android platform.

Source: Internet
Author: User

Obtain all available Sensors and androidsensors on a device on the Android platform.

I was going to write a small app that detects the temperature of a mobile phone. I want to learn about the sensor api, but I can't write the result. After detection, we found that the obtained Sensor is NULL, so we can see that my mobile phone does not have the TYPE_AMBIENT_TEMPERATURE Sensor.

So I wrote an APP to list all available Sensors. The Code is as follows:

 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 }

After compilation, install the package on your phone. The output does not show AMBIENT_TEMPERATURE. I have a temperature measuring software on my mobile phone that can detect the temperature. It may be the CPU temperature or other methods that I have not figured out yet. I have attached another piece of code for temperature measurement using a temperature sensor.

 
 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 }
 

 

 

This is 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>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.