Android Sensor Check Small demo

Source: Internet
Author: User

Last night, I wrote a very simple demo about sensor, that is, check your phone or tablet. There are no sensors detected. As I have been busy today, I will summarize it now.

First look at the run:


Samsung Note 3:


Samsung GT-N8000:



Here's the code:

Package Com.example.sensorcheck;import Android.app.activity;import Android.content.context;import Android.hardware.sensor;import Android.hardware.sensormanager;import Android.os.bundle;import Android.view.Menu; Import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;import Android.widget.toast;public class Main extends Activity { Private Sensormanager msensormanager;private String checkresult = "result:\n";p rivate TextView tvresult;private Button btncheck; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); tvresult = (TextView) Findviewbyid (r.id.tvresult); btncheck = (Button) Findviewbyid ( R.id.btncheck); Btncheck.setonclicklistener (new Onclicklistenerimp ());//Sensorcheck ();} Class Onclicklistenerimp implements Onclicklistener {@Overridepublic void OnClick (View v) {//TODO auto-generated method s Tubsensorcheck ();}} Private void Sensorcheck () {Msensormanager = (Sensormanager) getsystemservice (context.sensor_service);//ACCELEROMETERif ( Msensormanager.getdefaultsensor (sensor.type_accelerometer)! = null) {Checkresult = Checkresult + "Acceleration sensor: YES\n";} else { Checkresult = Checkresult + "Acceleration sensor: no\n";} Ambient_temperatureif (Msensormanager.getdefaultsensor (sensor.type_ambient_temperature)! = null) {CheckResult = Checkresult + "thermometer: yes\n";} else {Checkresult = Checkresult + "thermometer: no\n";} Game_rotation_vectorif (Msensormanager.getdefaultsensor (sensor.type_game_rotation_vector)! = null) {CheckResult = Checkresult + "Game rotation vector sensor: yes\n";} else {Checkresult = Checkresult + "Game rotation vector sensor: no\n";} Geomagnetic_rotation_vectorif (Msensormanager.getdefaultsensor (sensor.type_geomagnetic_rotation_vector)! = NULL {Checkresult = Checkresult + "Geomagnetic rotation vector sensor: yes\n";} else {Checkresult = Checkresult + "Geomagnetic rotation vector sensor: no\n";} Gravityif (Msensormanager.getdefaultsensor (sensor.type_gravity)! = null) {Checkresult = Checkresult + "Gravity sensor: yes\n ";} else {Checkresult = Checkresult + "Gravity sensor: no\n";} Gyroscopeif (Msensormanager.getdefaultsensor (sensor.type_gyroscope)! = null) {Checkresult = Checkresult + "Gyroscope: YES\n ";} else {Checkresult = Checkresult + "gyroscope: no\n";} Gyroscope_uncalibratedif (Msensormanager.getdefaultsensor (sensor.type_gyroscope_uncalibrated)! = null) { Checkresult = Checkresult + "Gyroscope not calibrated: yes\n";} else {Checkresult = Checkresult + "Gyro not calibrated: no\n";} Lightif (Msensormanager.getdefaultsensor (sensor.type_light) = null) {Checkresult = Checkresult + "Light sensor: YES\n";} els e {checkresult = Checkresult + "Light sensor: no\n";} Linear_accelerationif (Msensormanager.getdefaultsensor (sensor.type_linear_acceleration)! = null) {CheckResult = Checkresult + "Acceleration sensor: yes\n";} else {Checkresult = Checkresult + "Acceleration sensor: no\n";} Magnetic_fieldif (Msensormanager.getdefaultsensor (sensor.type_magnetic_field)! = null) {Checkresult = CheckResult + "Magnetic field sensor: yes\n";} else {Checkresult = Checkresult + "magnetic field sensor: no\n";} Magnetic_Field_uncalibratedif (Msensormanager.getdefaultsensor (sensor.type_magnetic_field_uncalibrated)! = null) { Checkresult = Checkresult + "Non-calibrated magnetic field sensor: yes\n";} else {Checkresult = Checkresult + "Non-calibrated magnetic field sensor: no\n";} Orientationif (Msensormanager.getdefaultsensor (sensor.type_orientation)! = null) {Checkresult = CheckResult + "Direction sensor : yes\n ";} else {Checkresult = Checkresult + "Direction sensor: no\n";} Pressureif (Msensormanager.getdefaultsensor (sensor.type_pressure)! = null) {Checkresult = Checkresult + "Pressure sensor: YES\n ";} else {Checkresult = Checkresult + "Pressure sensor: no\n";} Proximityif (Msensormanager.getdefaultsensor (sensor.type_proximity)! = null) {Checkresult = Checkresult + "Distance sensor: YES \ n ";} else {Checkresult = Checkresult + "Distance sensor: no\n";} Relative_humidityif (Msensormanager.getdefaultsensor (sensor.type_relative_humidity)! = null) {CheckResult = Checkresult + "Relative humidity sensor: yes\n";} else {Checkresult = Checkresult + "Relative humidity sensor: no\n";} Rotation_vectorif (Msensormanager.getdefaultsensor (sensor.type_rotation_vector)! = null) {Checkresult = Checkresult + "Rotation vector sensor: yes\n";} else {checkresult = Checkresult + "Rotation vector sensor: no\n";} Significant_motionif (Msensormanager.getdefaultsensor (sensor.type_significant_motion)! = null) {CheckResult = Checkresult + "Significant motion sensor: yes\n";} else {Checkresult = Checkresult + "Significant motion sensor: no\n";} Step_counterif (Msensormanager.getdefaultsensor (sensor.type_step_counter)! = null) {Checkresult = CheckResult + " Step sensor: Yes\n ";} else {Checkresult = Checkresult + "Step sensor: no\n";} Step_detectorif (Msensormanager.getdefaultsensor (sensor.type_step_detector)! = null) {Checkresult = CheckResult + " Step detector: yes\n ";} else {Checkresult = Checkresult + "Step detector: no\n";} Temperatureif (Msensormanager.getdefaultsensor (sensor.type_temperature)! = null) {Checkresult = CheckResult + "Temperature sensor : yes\n ";} else {Checkresult = Checkresult + "Temperature sensor: no\n";} Tvresult.settext (Checkresult);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action BarIf it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}


Layout:

<linearlayout 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:orientation= "vertical"    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= "Com.example.sensorcheck.Main" >    <textview        android:id= "@+id/tvresult"        android:textsize= "20DP"        android:layout_width= "wrap _content "        android:layout_height=" wrap_content "        android:text=" @string/hello_world "/>    < Button        android:id= "@+id/btncheck"        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:text=" Check Sensor "/></linearlayout>

Reprint Please specify source: Zhou Mushi's csdn blog Http://blog.csdn.net/zhoumushui

My github: Zhou Mushi's GitHub Https://github.com/zhoumushui





Android Sensor Check Small demo

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.