gpt4 book ai didi

android - 如何注销传感器监听器?

转载 作者:搜寻专家 更新时间:2023-11-01 07:51:41 25 4
gpt4 key购买 nike

我有 AsyncTask,在 doInBackground 中我设置了一个 timerTask,它实例化下面发布的 UpdateLogFile 类。正如你在类里面看到的我注册了一个传感器监听器并在 onCancelled 和 onPostExecute 中注销了它,但问题是传感器 lsitener 总是被注册并且不能取消注册,因为即使我取消 AsyncTask 我仍然收到 Log.w(TAG, CSubTag.msg("onSensorChanged"));

请告诉我发生这种情况的原因以及如何正确取消注册。

代码:

    @Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.w(TAG, CSubTag.msg("ATRx.onPostExecute"));

mSenMgr.unregisterListener(mSEL);
}
..
..
..
private class UpdateLogFile extends TimerTask {

private File mLogFile = null;
private long mStartTs;
private double mLng;
private double mLat;
private long mTs;

UpdateLogFile(File logFile, long startTs) {
this.mLogFile = logFile;
this.mStartTs = startTs;

mSenMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensAcc = mSenMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensMag = mSenMgr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

mSEL = new SensorEventListener() {
float[] mGravity;
float[] mGeomagnetic;

@Override
public void onSensorChanged(SensorEvent event) {
Log.w(TAG, CSubTag.msg("onSensorChanged"));

Sensor mySensor = event.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mGravity = event.values;
}
if (mySensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mGeomagnetic = event.values;
}

if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
mAzimuth = orientation[0];
mPitch = orientation[1];
mRoll = orientation[2];
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mSenMgr.registerListener(mSEL, mSensAcc, SensorManager.SENSOR_DELAY_NORMAL);
mSenMgr.registerListener(mSEL, mSensMag, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void run() {

long endTs = TimeUtils.getTSSec();
this.mTs = endTs - this.mStartTs;

this.mLng = mGPSCtrl.getLng();
this.mLat = mGPSCtrl.getLat();

IOCtrl.writeLog(this.mLogFile, this.mTs, this.mLng, this.mLat, mAzimuth, mPitch, mRoll, mFMSMsg1, mFMSMsg2, mFMSMsg3, mFMSMsg4, mFMSMsg7, mFMSMsg8, mFMSMsg12);
Log.d(TAG, CSubTag.msg("UpdateTimer.run", "line: " + this.mLng + "," + this.mLat + "," + mAzimuth + "," + mPitch + "," + mRoll + "," + mFMSMsg1 + "," + mFMSMsg2 + "," + mFMSMsg3 + "," + mFMSMsg4 + "," + mFMSMsg7 + "," + mFMSMsg8 + "," + mFMSMsg12));
}
}

最佳答案

注销监听时是否检查过mSEL是否为空?

如果你想监听传感器,你应该创建一个 Activity/Service/AsyncTask/... 来实现android.hardware.SensorEventListener 接口(interface)。 (此 page 介绍了 SensorEventListeners。)然后您可以随时取消注册监听器 (mSenMgr.unregisterListener(this))。

尝试以下操作:

public class CustomTask extends AsyncTask<SensorManager, Void, Void> implements SensorEventListener {
private SensorManager sensorManager;

@Override
public void onSensorChanged(SensorEvent event) { }

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { }

@Override
protected Void doInBackground(SensorManager... params) {
sensorManager = params[0];
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
sensorManager.unregisterListener(this);
}
}

这是注册监听器的代码:

SensorManager mSenMgr = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
Sensor mSensAcc = mSenMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
CustomTask task = new CustomTask();
task.doInBackground(mSenMgr);
mSenMgr.registerListener(task, mSensAcc, SensorManager.SENSOR_DELAY_NORMAL);

关于android - 如何注销传感器监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com