Android simulator framework and driver sensor Article 4 (Android APP)

Source: Internet
Author: User

The above has already introduced the porting process of android temperature sensor. The code page has been posted to everyone. Now we can write an APK for testing. The code is very simple, and the interface is very simple and ugly. Haha, don't mind. This aok is only used for testing. I will not introduce it more here and paste the Code directly.
 
 
 
Package com. android. jay. sensor1;
 
Import android. app. Activity;
Import android. hardware. Sensor;
Import android. hardware. SensorEvent;
Import android. hardware. SensorEventListener;
Import android. hardware. SensorManager;
Import android. OS. Bundle;
Import android. widget. TextView;
 
Public class MySensor1Activity extends Activity implements SensorEventListener {
/** Called when the activity is first created .*/
Private TextView tView;
SensorManager sensorManager = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TView = (TextView) findViewById (R. id. TV );
SensorManager = (SensorManager) getSystemService (SENSOR_SERVICE );
SensorManager. registerListener (this, sensorManager. getdefasensensor (Sensor. TYPE_TEMPERATURE), sensorManager. SENSOR_DELAY_GAME );
}

Public void onSensorChanged (SensorEvent event ){
If (event. sensor. getType () = Sensor. TYPE_TEMPERATURE)
TView. setText ("value:" + Float. toString (event. values [0]);

}
 
Public void onAccuracyChanged (Sensor sensor, int accuracy ){
// TODO Auto-generated method stub

}
}
Package com. android. jay. sensor1;
 
Import android. app. Activity;
Import android. hardware. Sensor;
Import android. hardware. SensorEvent;
Import android. hardware. SensorEventListener;
Import android. hardware. SensorManager;
Import android. OS. Bundle;
Import android. widget. TextView;
 
Public class MySensor1Activity extends Activity implements SensorEventListener {
/** Called when the activity is first created .*/
Private TextView tView;
SensorManager sensorManager = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TView = (TextView) findViewById (R. id. TV );
SensorManager = (SensorManager) getSystemService (SENSOR_SERVICE );
SensorManager. registerListener (this, sensorManager. getdefasensensor (Sensor. TYPE_TEMPERATURE), sensorManager. SENSOR_DELAY_GAME );
}

Public void onSensorChanged (SensorEvent event ){
If (event. sensor. getType () = Sensor. TYPE_TEMPERATURE)
TView. setText ("value:" + Float. toString (event. values [0]);

}
 
Public void onAccuracyChanged (Sensor sensor, int accuracy ){
// TODO Auto-generated method stub

}
}
Finally, use adb install xxx.apk to install the program and then run it on the simulator. The result is as follows:
 
 
 
 

 


 
 
OK. We can see that the value is constantly changing. It is finished. The following section describes how to deal with the framework layer...

From the column zhangjie201412

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.