如何获取psensor两次距离变化的时间差然后取消倒计时

获取psensor两次距离变化的时间差,不满2秒取消倒计时不起作用
Camera还是起来了,后面的Activity也跳转了
怎么回事
网上说是5.0以前不能CountDownTimer直接cancel,会不起作用。
但我开发环境6.0的,手机8.0的,为什么还是没用。

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

public class SensorFunctionService extends Service {

private SensorManager mSensorManager;
private Sensor mPSensor;
private PowerManager mPowerManager;
private PowerManager.WakeLock mProximityWakeLock;
private CountDownTimer timer;
private long mLastProximityEventTime = 0;
private long milliseconds;

@Override
public void onCreate() {
super.onCreate();
mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
mProximityWakeLock = mPowerManager.newWakeLock(32,"csl");
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mPSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

if (null == mSensorManager) {
    Log.d("csl", "deveice not support SensorManager");
}
SensorEventListener mSensorEventListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {

        Log.d("csl", "onSensorChanged 。。。。");
        if (event.sensor == null) {
            return;
        }
        if (event.sensor.getType() == Sensor.TYPE_PROXIMITY){


            float distance = event.values[0];

           if (distance == 0.0) {
                milliseconds = SystemClock.elapsedRealtime();
            } else {
                mLastProximityEventTime = SystemClock.elapsedRealtime();
            }
            final long timeSinceLastEvent =  mLastProximityEventTime - milliseconds;

            Log.d("xxxx"," data: " + distance +" +"+ milliseconds + " +"+mLastProximityEventTime + " +"+timeSinceLastEvent);

            if (distance == 0.0) {
                timer = new CountDownTimer(2000,1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {

                        if ((timeSinceLastEvent < 2000) && (timeSinceLastEvent >0)) {
                            timer.cancel();
                        }
                    }

                    @Override
                    public void onFinish() {

                        //Intent intent = new Intent(SensorFunctionService.this, Main2Activity.class);
                        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                        //startActivity(intent);
                        Intent  cameraIntent = new Intent();
                        // 指定开启系统相机的Action
                        cameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                        cameraIntent.addCategory(Intent.CATEGORY_DEFAULT);
                        cameraIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                        startActivity(cameraIntent);

                        timer = new CountDownTimer(5000,1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {

                            }

                            @Override
                            public void onFinish() {

                                Intent intent = new Intent(SensorFunctionService.this, Main2Activity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                                startActivity(intent);
                                Toast.makeText(SensorFunctionService.this,"onFinish", Toast.LENGTH_SHORT).show();
                            }
                        }.start();
                    }
                }.start();

            }
        }

    }



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

    }
};
mSensorManager.registerListener(mSensorEventListener, mPSensor, SensorManager.SENSOR_DELAY_NORMAL);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

new Thread(new Runnable() {
    @Override
    public void run() {

    }
});
return super.onStartCommand(intent, flags, startId);

}

@Override
public void onDestroy() {
super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

}

1个回答

解决了。
因为每次sensorChanged就重跑timer了,所以两个时间一个在onTick之前,一个在onTick之后,所以在onTick里面始终获取不到两个值的差。
所以不去折腾时间了。自己设一个变量,onTick的时候设为true,为true的时候Psensor远离就停止倒计时。onFinish的时候设为false

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问