Android obtains all sensor data

Source: Internet
Author: User
Tags ibm developerworks

Main. xml

 

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "acceleration" Android: Id = "@ + ID/edt1"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "magnetic field" Android: Id = "@ + ID/edt2"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "" Android: Id = "@ + ID/edt3"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "gyroscope" Android: Id = "@ + ID/edt4"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "light" Android: Id = "@ + ID/edt5"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "" Android: Id = "@ + ID/edt6"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "temperature" Android: Id = "@ + ID/edt7"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "distance" Android: Id = "@ + ID/edt8"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "gravity" Android: Id = "@ + ID/edt9"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "linear acceleration" Android: Id = "@ + ID/edt10"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "rotating vector" Android: Id = "@ + ID/edt11"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "defalut" Android: Id = "@ + ID/edt12"/> </linearlayout>

 

 

 

 

Main. Java

/*
*
* Ibmeyes. Java
* Sample code for IBM developerworks article
* Author: W. Frank ableson
* Fableson@msiservices.com
*
*/

Package com. MSI. IBM. eyes;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. widget. textview;
Import Android. Hardware. sensor;
Import Android. Hardware. sensormanager;
Import Android. Hardware. sensorlistener;

Public class ibmeyes extends activity implements sensorlistener {

Final string tag = "ibmeyes ";
Sensormanager Sm = NULL;

Textview view1 = NULL;
Textview view2 = NULL;
Textview view3 = NULL;
Textview view4 = NULL;
Textview view5 = NULL;
Textview view6 = NULL;
Textview view7 = NULL;
Textview view8 = NULL;
Textview view9 = NULL;
Textview view10 = NULL;
Textview view11 = NULL;
Textview view12 = NULL;

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Sm = (sensormanager) getsystemservice (sensor_service );
Setcontentview (R. layout. Main );
View1 = (textview) findviewbyid (R. Id. edt1 );
View2 = (textview) findviewbyid (R. Id. edt2 );
View3 = (textview) findviewbyid (R. Id. edt3 );
View4 = (textview) findviewbyid (R. Id. edt4 );
View5 = (textview) findviewbyid (R. Id. edt5 );
View6 = (textview) findviewbyid (R. Id. edt6 );
View7 = (textview) findviewbyid (R. Id. edt7 );
View8 = (textview) findviewbyid (R. Id. edt8 );
View9 = (textview) findviewbyid (R. Id. edt9 );
View10 = (textview) findviewbyid (R. Id. edt10 );
View11 = (textview) findviewbyid (R. Id. edt11 );
View12 = (textview) findviewbyid (R. Id. edt12 );
}

Public void onsensorchanged (INT sensor, float [] values ){
Synchronized (this ){
String STR = "X:" + values [0] + ", Y:" + values [1] + ", Z:" + values [2];
Switch (sensor ){
Case sensor. type_accelerometer:
View1.settext ("Acceleration:" + Str );
Break;
Case sensor. type_magnetic_field:
View2.settext ("magnetic field:" + Str );
Break;
Case sensor. type_orientation:
View3.settext ("Positioning:" + Str );
Break;
Case sensor. type_gyroscope:
View4.settext ("gyroscope:" + Str );
Break;
Case sensor. type_light:
View5.settext ("Light:" + Str );
Break;
Case sensor. type_pressure:
View6.settext ("Pressure:" + Str );
Break;
Case sensor. type_temperature:
View7.settext ("temperature:" + Str );
Break;
Case sensor. type_proximity:
View8.settext ("distance:" + Str );
Break;
Case sensor. type_gravity:
View9.settext ("gravity:" + Str );
Break;
Case sensor. type_linear_acceleration:
View10.settext ("linear acceleration:" + Str );
Break;
Case sensor. type_rotation_vector:
View11.settext ("rotating vector:" + Str );
Break;
Default:
View12.settext ("Normal:" + Str );
Break;
}
}
}

Public void onaccuracychanged (INT sensor, int accuracy ){
Log. D (TAG, "onaccuracychanged:" + sensor + ", accuracy:" + accuracy );
}

@ Override
Protected void onresume (){
Super. onresume ();
SM. registerlistener (this,
Sensor. type_accelerometer |
Sensor. type_magnetic_field |
Sensor. type_orientation |
Sensor. type_gyroscope |
Sensor. type_light |
Sensor. type_pressure |
Sensor. type_temperature |
Sensor. type_proximity |
Sensor. type_gravity |
Sensor. type_linear_acceleration |
Sensor. type_rotation_vector,
Sensormanager. sensor_delay_normal );
}

@ Override
Protected void onstop (){
SM. unregisterlistener (this );
Super. onstop ();
}

}

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.