msensor的应用部分:
public class MSensor extends Activity
implements SensorEventListener
{
private View.OnClickListener cl;
private Button mBtFailed;
private Button mBtOk;
private float mDegressQuondam = 0;
private ImageView mImgCompass = null;
private RotateAnimation mMyAni = null;
private TextView mOrientText = null;
private TextView mOrientValue = null;
private SensorManager mSm = null;
private SharedPreferences mSp;
private String text = "";
public Sensor mSensor;
public SensorManager mSensorManager;
private String _message = "";
private float _decDegree = 0;
/*
重新画图片
*/
private void AniRotateImage(float paramFloat)
{
float k = this.mDegressQuondam;
{
RotateAnimation localRotateAnimation1 = new RotateAnimation(mDegressQuondam,-paramFloat,
RotateAnimation.RELATIVE_TO_SELF,0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
//(k,paramFloat, 1, 1056964608, 1, 1056964608);
this.mMyAni = localRotateAnimation1;
this.mMyAni.setDuration(1000L);
this.mMyAni.setFillAfter(true);
ImageView localImageView = this.mImgCompass;
RotateAnimation localRotateAnimation2 = this.mMyAni;
localImageView.startAnimation(localRotateAnimation2);
//this.mDegressQuondam = paramFloat;
this.mDegressQuondam = -paramFloat;
mOrientText.setText(_message);
TextView localTextView = this.mOrientValue;
StringBuilder localStringBuilder1 = newStringBuilder(text).append(paramFloat);
localTextView.setText(localStringBuilder1.toString());
}
}
/*
采样精度的变化,一般驱动初始化的时候就固定下来。没有特殊需求就不修改处理了。
*/
public void onAccuracyChanged(Sensor paramSensor, int paramInt)//publicvoid onAccuracyChanged(int paramInt1, int paramInt2)
{
}
public void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.msensor);
this.mSp = getSharedPreferences("FactoryMode", 0);
this.mOrientText = (TextView)findViewById(R.id.OrientText);
this.mImgCompass = (ImageView)findViewById(R.id.ivCompass);
this.mOrientValue = (TextView)findViewById(R.id.OrientValue);
this.mBtOk = (Button)findViewById(R.id.bt_ok);
this.mBtOk.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Context localContext = MSensor.this.getApplicationContext();
SharedPreferences localSharedPreferences = MSensor.this.mSp;
Utils.SetPreferences(localContext, localSharedPreferences,R.string.msensor_name, "success");
MSensor.this.finish();
}
});
this.mBtFailed = (Button)findViewById(R.id.bt_failed);
this.mBtFailed.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Context localContext = MSensor.this.getApplicationContext();
SharedPreferences localSharedPreferences = MSensor.this.mSp;
Utils.SetPreferences(localContext, localSharedPreferences, R.string.msensor_name, "failed");
MSensor.this.finish();
}
});
getWindow().setFlags(128, 128);
}
public void onDestroy()
{
//this.mSm.unregisterListener(this);
super.onDestroy();
}
protected void onResume()
{
/*super.onResume();
this.mSm.registerListener(this, 1, 0);
*/
super.onResume();
SensorManagerlocalSensorManager = this.mSensorManager;
Sensor localSensor= this.mSensor;
/*
给onStart中获取的SensorManager设置了一个监听器。
*/
localSensorManager.registerListener(this, localSensor, 3);
}
public void setDegree(float degree)
{
if(Math.abs(_decDegree - degree) >= 2 )
{
_decDegree = degree;
//int range = 1;
String degreeStr = String.valueOf(_decDegree);
StringBuilder sb = new StringBuilder();
if(_decDegree ==0||_decDegree==360)//(_decDegree > 360 - range && _decDegree < 360 +range)
{
_message =getString(R.string.MSensor_North);
}
if(_decDegree ==90)//(_decDegree > 90 - range && _decDegree
{
_message =getString(R.string.MSensor_East);
}
if(_decDegree ==180)//(_decDegree > 180 - range && _decDegree< 180 + range)
{
_message = getString(R.string.MSensor_South);
}
if(_decDegree ==270)//(_decDegree > 270 - range && _decDegree< 270 + range)
{
_message=getString(R.string.MSensor_West);
}
if(_decDegree > 0 && _decDegree < 90)//(_decDegree > 45- range && _decDegree < 45 + range)
{
_message =sb.append(getString(R.string.MSensor_north_east)).append(degreeStr).append(getString(R.string.MSensor_degree)).toString();
}
if(_decDegree > 90 && _decDegree < 180)//(_decDegree >135 - range && _decDegree < 135 + range)
{
_message =sb.append(getString(R.string.MSensor_south_east)).append(degreeStr).append(getString(R.string.MSensor_degree)).toString();;
}
if(_decDegree > 180 && _decDegree < 270)//(_decDegree >225 - range && _decDegree < 225 + range)
{
_message =sb.append(getString(R.string.MSensor_south_west)).append(degreeStr).append(getString(R.string.MSensor_degree)).toString();
}
if(_decDegree > 270 && _decDegree < 360)//(_decDegree >315 - range && _decDegree < 315 + range)
{
_message =sb.append(getString(R.string.MSensor_north_west)).append(degreeStr).append(getString(R.string.MSensor_degree)).toString();
}
AniRotateImage(_decDegree);
}
}
//public void onSensorChanged(int paramInt,float[] values)
public void onSensorChanged(SensorEvent paramSensorEvent)
{
if (paramSensorEvent.sensor.getType() ==Sensor.TYPE_ORIENTATION)
setDegree(paramSensorEvent.values[SensorManager.DATA_X]);
}
public void onStart()
{
/*super.onStart();
this.mSm = (SensorManager)getSystemService("sensor");
*/
/*
开机会启动一个sensor的服务,这里是通过该服务实例化一个SensorManager
*/
SensorManagerlocalSensorManager = (SensorManager)getSystemService("sensor");
this.mSensorManager = localSensorManager;
/*
刚刚获取的SensorManager中得到需要的senosr类型,这里的3就是指南针Msensor
*/
Sensor localSensor= this.mSensorManager.getDefaultSensor(3);
this.mSensor =localSensor;
super.onStart();
}
protected void onStop()
{
/*this.mSm.unregisterListener(this);
super.onStop();*/
this.mSensorManager.unregisterListener(this);
super.onStop();
}
public void onClick(View v) {
Context localContext = this.getApplicationContext();
SharedPreferences localSharedPreferences = this.mSp;
if(v.getId() == this.mBtOk.getId()){
Utils.SetPreferences(localContext, localSharedPreferences,R.string.msensor_name, "success");
finish();
}
else{
Utils.SetPreferences(localContext, localSharedPreferences,R.string.msensor_name, "failed");
finish();
}
}
}
个人理解:
应用获取MSENSOR数据的方法大概是下面几个步骤:
1,通过sensor的服务获取一个SensorManager.
2, 在通过这个SensorManager获取指定类型的SENSOR,这里定义的是MSENSOR,编号为3.
3,SensorManager设置一个对该SENSOR类型的一个监听。
4,监听到有数据变化,就会运行onSensorChanged,所以后面获取到数据的处理,就可以放在这个函数里面进行。这里主要介绍数据的获取,UI的东西先忽略掉。