Android模拟器学framework和driver之传感器篇4(Android APP)
上面已经介绍了,android temperature sensor的移植过程,代码页已经贴给大家了,现在我们写一个APK来进行测试,代码很简单,界面也很简单很丑陋,哈哈,大家不要介意,这个aok只是用作测试,这里我就不多做介绍了,直接贴代码。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.getDefaultSensor(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.getDefaultSensor(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
}
}
最后用adb install xxx.apk安装程序,然后再模拟器上运行,结果如下:
OK,看到value在不断的变化了吧,完工,下面一节我们会介绍framework层如何处理的。。。
摘自 zhangjie201412的专栏
补充:移动开发 , Android ,