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