安卓中如何使用Sensor simulator 在模拟器上进行传感器开发

大家好啊,这是本人的第一篇博客,最近在搞安卓的传感器开发,在这篇文章里我详述了基于SensorSimulator的传感器开发步骤。使用SensorSimulator可以在没有手机的情况下在电脑上模拟手机的姿态,来测试比如像用到方向传感器一类的程序。希望对大家有所帮助。

0.启动eclipse,新建2.3.3版本的工程,启动2.2.3版本的模拟器。

1.SensorSimulator的安装

登录http://code.google.com/p/openintents/downloads/list?q=sensorsimulator下载SensorSimulator-2.0,解压后里面有很多文件夹,其中“bin”“lib”是最有用的两个文件夹。将所有的文件夹放到D盘的根目录下。

打开CMD窗口,进入D盘下的“bin”文件夹。

键入adb install SensorSimulatorSettings-2.0-rc1.apk为模拟器安装该程序。

 

bin文件夹下找到可执行的JAR文件--sensorsimulator-2.0-rc1.jar,双击打开它。

 

橙色的字是本机的IP地址,一会儿要用到。

在安卓模拟器中找到刚才新安装的SettingsAPK,打开之后,进行相关设置,可参考http://blog.csdn.net/achellies/article/details/6782954中的第4步。这里要注意IP地址的设置,上面列出的橙色的地址有好几个,如果第一个不行,可以试试后面几个。连接完成后就可以编写程序了。

2.导入第三方的JAR

在编写程序的时候,要用到第三方的JAR包,需要将“lib”文件夹里的JAR包导入自己的安卓工程。笔者在这一步上花了很多时间,很多导入JAR包的方法都不正确,程序运行时会报错。

最终我找到的方法来自:http://blog.csdn.net/ibznphone/article/details/7875532按照上面的方法将sensorsimulator-lib-2.0-rc1.jar导入工程。

3.编写程序

这里我将自己的程序附上,希望对大家有所帮助。

package com.example.sensortest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;


public class SensorMainActivity extends Activity {
private SensorManagerSimulator mSensorManager;
private Sensor mSensor;
private TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor_main);
text=(TextView)findViewById(R.id.text);
mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
mSensorManager.connectSimulator();
mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

}
@Override
protected void onResume()
{
mSensorManager.registerListener(mListener, mSensor, SensorManagerSimulator.SENSOR_DELAY_GAME);
super.onResume();
}
@Override
protected void onPause()
{
mSensorManager.unregisterListener(mListener);
super.onPause();
}


private SensorEventListener mListener=new SensorEventListener()
{

public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO 自动生成的方法存根

}

public void onSensorChanged(SensorEvent event) {
// TODO 自动生成的方法存根
text.setText("X坐标:"+event.values[0]+"Y坐标:"+event.values[1]+"Z坐标:"+event.values[2]);
}



};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_sensor_main, menu);
return true;
}
}

 

猜你喜欢

转载自blog.csdn.net/f1jiaziqing/article/details/8960668