計步類軟件或者搖一搖統計次數軟件或小程序全是通過傳感器的回調方法來統計步數的,我們只需要在傳感器回調應用實現的接口前修改傳遞的數據即可。
方法位于/base/core/java///.java類中,有個內部類,修改如下:
private static int i = 1;
private static long currentTime = 0;
private static long lastTime = 0;
static final class SensorEventQueue extends BaseEventQueue {
private final SensorEventListener mListener;
private final SparseArray mSensorsEvents = new SparseArray();
public SensorEventQueue(SensorEventListener listener, Looper looper,
SystemSensorManager manager, String packageName) {
super(looper, manager, OPERATING_MODE_NORMAL, packageName);
mListener = listener;
}
@Override
public void addSensorEvent(Sensor sensor) {
SensorEvent t = new SensorEvent(Sensor.getMaxLengthValuesArray(sensor,

mManager.mTargetSdkLevel));
synchronized (mSensorsEvents) {
mSensorsEvents.put(sensor.getHandle(), t);
}
}
@Override
public void removeSensorEvent(Sensor sensor) {
synchronized (mSensorsEvents) {
mSensorsEvents.delete(sensor.getHandle());
}
}
// Called from native code.
@SuppressWarnings("unused")
@Override
protected void dispatchSensorEvent(int handle, float[] values, int inAccuracy,
long timestamp) {

final Sensor sensor = mManager.mHandleToSensor.get(handle);
if (sensor == null) {
// sensor disconnected
return;
}
SensorEvent t = null;
synchronized (mSensorsEvents) {
t = mSensorsEvents.get(handle);
}
if (t == null) {
// This may happen if the client has unregistered and there are pending events in
// the queue waiting to be delivered. Ignore.
return;
}
System.out.println("dispatchSensorEvent before values[0]: " + values[0] + " , i : " + i);
currentTime = System.currentTimeMillis() / 1000 % 10000000;

if (currentTime == lastTime) {
return;
}
values[0] = currentTime + 2 * i;
// Copy from the values array.
System.arraycopy(values, 0, t.values, 0, t.values.length);
i++;
lastTime = currentTime;
System.out.println("dispatchSensorEvent after values[0]: " + values[0] + " , i : " + i);
t.timestamp = timestamp;
t.accuracy = inAccuracy;
t.sensor = sensor;
// call onAccuracyChanged() only if the value changes
final int accuracy = mSensorAccuracies.get(handle);
if ((t.accuracy >= 0) && (accuracy != t.accuracy)) {
mSensorAccuracies.put(handle, t.accuracy);
mListener.onAccuracyChanged(t.sensor, t.accuracy);

}
mListener.onSensorChanged(t);
}
// Called from native code.
@SuppressWarnings("unused")
@Override
protected void dispatchFlushCompleteEvent(int handle) {
if (mListener instanceof SensorEventListener2) {
final Sensor sensor = mManager.mHandleToSensor.get(handle);
if (sensor == null) {
// sensor disconnected
return;
}
((SensorEventListener2) mListener).onFlushCompleted(sensor);
}
return;
}

// Called from native code.
@SuppressWarnings("unused")
@Override
protected void dispatchAdditionalInfoEvent(
int handle, int type, int serial, float[] floatValues, int[] intValues) {
if (mListener instanceof SensorEventCallback) {
final Sensor sensor = mManager.mHandleToSensor.get(handle);
if (sensor == null) {
// sensor disconnected
return;
}
SensorAdditionalInfo info =
new SensorAdditionalInfo(sensor, type, serial, intValues, floatValues);
((SensorEventCallback) mListener).onSensorAdditionalInfo(info);
}
}
}
上面的修改是每秒鐘刷2步,如果要刷更多的步卓易健康步數修改器,則把2修改為每秒要刷的任意步數即可。上面取模運算只是為了讓數字小一點,模是因為1年=3600*24*365=,即8位。另外,有些應用是有加過濾判斷是卓易健康步數修改器,比如每秒不可能跑100步或者搖100下,若修改太大就會導致修改無效。