Android重力感应模拟器实现与实例开发(转)

原址[url]http://wxmijl.blog.163.com/blog/static/13245928201062734631474/[/url]
Sensor Simulator是重力感应模拟器,下载地址如下:
[url]http://code.google.com/p/openintents/downloads/detail?name=sensorsimulator-1.0.0-beta1.zip&can=2&q=[/url]



在你的就应用里怎样使用 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值