原址
http://wxmijl.blog.163.com/blog/static/13245928201062734631474/
Sensor Simulator是重力感应模拟器,下载地址如下:
http://code.google.com/p/openintents/downloads/detail?name=sensorsimulator-1.0.0-beta1.zip&can=2&q=
在你的就应用里怎样使用 SensorSimulator
如果你从没使用过,请在你的模拟器中安装OpenIntents.apk
Add the external JAR openintents-lib-n.n.n.jar into your project.
Replace the following code in onCreate():
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
by this code
// Before calling any of the Simulator data,
// the Content resolver has to be set !!
Hardware.mContentResolver = getContentResolver();
// Link sensor manager to OpenIntents Sensor simulator
mSensorManager = (SensorManager) new SensorManagerSimulator((SensorManager)
getSystemService(SENSOR_SERVICE));
By default this still passes the original sensor values, so to activate sensors you have to first call (to set the sensor settings):
Intent intent = new Intent(Intent.ACTION_VIEW,
Hardware.Preferences.CONTENT_URI);
startActivity(intent);
and then
// first disable the current sensor
mSensorManager.unregisterListener(mGraphView);
// now connect to simulator
SensorManagerSimulator.connectSimulator();
// now enable the new sensors
mSensorManager.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER |
SensorManager.SENSOR_MAGNETIC_FIELD |
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_FASTEST);
All other code stays untouched. You can find reference code for how to implement sensors in the API demos / OS / Sensors.java.
Usually one would (un)register the sensors in onResume() and onStop():
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER |
SensorManager.SENSOR_MAGNETIC_FIELD |
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(mGraphView);
super.onStop();
}
Then just implement the standard Android SensorListener (there is no OI counterpart necessary).
class MySensorActivity extends Activity implements SensorListener {
public void onSensorChanged(int sensor, float[] values) {
// do something with the sensor values.
}
}
Note 1: The OpenIntents class SensorManagerSimulator is derived from the Android class SensorManager and implements exactly the same functions (see Android SensorManager). For the callback, the standard Android SensorListener is used. The only new functions are connectSimulator() and disconnectSimulator().
Note 2: Whenever you are not connected to the simulator, you will get real device sensor data: the org.openintents.hardware.SensorManagerSimulator class transparently calls the SensorManager returned by the system service in this case.
分享到:
相关推荐
android 利用重力感应监听 微修改代码可以解决 来电时翻转手机后静音。
本程序是实现Android手机控制小车程序的前一段重力感应的源程序。
Android 重力感应游戏-疯狂的小球源码程序,基于Android 平台的重力感应游戏-疯狂的小球。
android重力感应源代码
Android 重力感应 示例 源码 sensor
一款android重力感应源代码,里面有详细的讲解。
Android重力感应Demo
Android 重力感应游戏-疯狂的小球
android重力感应开发demo
Android手机平台重力感应Demo Android手机平台重力感应Demo Android手机平台重力感应Demo
android_重力感应demo,主要打印重力感应x,y,z,适合初学者研究重力感应数据变化规律
Android重力感应跑步测速应用.rar
android 重力感应摇骰子游戏 ,在真机上运行
Android 重力感应传感器控制源码,作者信息:Himi,加速度传感器 ,也称为重力传感器,源代码编译环境需要SDK 1.5(api 3)支持。此重力感应传感器不仅对玩家旋转手机的动作可以检测到,而且会根据反转手机的程度,得到...
重力感应装置包括感应器、处理器和控制器三个部分。感应器负责侦测存储器的状态,计算存储器的重力加速度值;处理器则对加速度值是否超出安全范围进行判断;而控制器则负责控制将磁头锁定或者释放出安全停泊区。一旦...
android禁止调用oncreat方法
Android重力感应跑步测速应用.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android 重力感应控件一个可以重力感应更新试图的控件可以用来当作 emptyview 和 errorview