简体   繁体   English

如何注销传感器侦听器?

[英]How to unregister a sensor listener?

I have AsyncTask and in doInBackground i set a timerTask that instantiates the UpdateLogFile class which is posted below. 我有AsyncTask,并在doInBackground中设置了一个timerTask来实例化下面发布的UpdateLogFile类。 As you see in that class i registered a sensor listener and unregister it in onCancelled and in onPostExecute, but the problem is the sensor lsitener is always registered and can't be unregistered because even if i cancel the AsyncTask i still receive the Log.w(TAG, CSubTag.msg("onSensorChanged")); 如您在该类中看到的,我注册了一个传感器侦听器,并在onCancelled和onPostExecute中将其注销,但是问题是传感器lsitener始终被注册并且不能被注销,因为即使我取消了AsyncTask我仍然会收到Log.w (TAG,CSubTag.msg(“ onSensorChanged”)));

Please let me know why that is happening and how to unregister it correctly. 请让我知道为什么会这样以及如何正确注销它。

code : 代码

    @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));
        }
    }

Have you checked whether mSEL is null when you unregister the listener? 取消注册侦听器时,是否检查mSEL是否为null?

If you want to listen on sensors you should either create an Activity/Service/AsyncTask/... that implements the android.hardware.SensorEventListener interface. 如果您想监听传感器,则应该创建一个Activity / Service / AsyncTask / ...来实现android.hardware.SensorEventListener接口。 (This page gives an introduction into SensorEventListeners.) Then you can simply unregister the listener ( mSenMgr.unregisterListener(this) ) at any time. (此页面对SensorEventListeners进行了介绍。)然后,您可以随时随时取消注册侦听器( mSenMgr.unregisterListener(this) )。

Try the following: 请尝试以下操作:

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);
  }
}

This is the code for registering the listener: 这是用于注册侦听器的代码:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM