Android的线程,无法注销SensorEventListener线程、Android、SensorEventListener

由网友(愛你再深終為過客)分享简介:我似乎有同样的问题,因为这位先生:SensorEventListener没有得到取消注册unregisterListener()方法 I seem to be having the same problem as this gentleman:SensorEventListener doesn't get unr...

我似乎有同样的问题,因为这位先生: SensorEventListener没有得到取消注册unregisterListener()方法

I seem to be having the same problem as this gentleman: SensorEventListener doesn't get unregistered with unregisterListener() method

public void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            ...
            loopThread();
}

private void loopThread() {
            boolean found = kill;
            System.out.println("Looping until Found!");
            locationThread = new Thread(){
                    private float myacc;

                    @Override
                    @Override
                    public void run() {

                            while (found == false && !kill){
                                    System.out.println("Another Loop!");
                                    getMyLoc();
                                    found = isFound(mylat,mylng,target_lat,target_lng,radius);
                                    try {
                                            synchronized(locationThread){
                                                    locationThread.wait(100);
                                            }

                                    } catch (InterruptedException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                    }
                            }
                            mSensorManager.unregisterListener(senslistener, mAccelerometer);
                            mSensorManager.unregisterListener(senslistener, mGravitometer);
                            mSensorManager.unregisterListener(senslistener);
                            locationThread.interrupt();
                    }
}

@Override
    public void onPause(){
            super.onPause();
            mSensorManager.unregisterListener(senslistener, mAccelerometer);
            mSensorManager.unregisterListener(senslistener, mGravitometer);
            locationThread.interrupt();
    }
    @Override
    public void onResume(){
            super.onResume();
            senslistener = new mySensorEventListener();
            mSensorManager.registerListener(senslistener, mAccelerometer,     SensorManager.SENSOR_DELAY_NORMAL);
            mSensorManager.registerListener(senslistener, mGravitometer, SensorManager.SENSOR_DELAY_NORMAL);
            if (true)
                    loopThread();
    }

private void registerSensors() {
            mSensorManager =     (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
            List<Sensor> mySensors = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
            if(mySensors.size() > 0){
                    mAccelerometer =     mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                    mGravitometer =     mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

                    mSensorManager.registerListener(senslistener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
                    mSensorManager.registerListener(senslistener, mGravitometer, SensorManager.SENSOR_DELAY_NORMAL);
                    Toast.makeText(context, "Getting your location...", Toast.LENGTH_LONG).show();
            }
            else{
                    Toast.makeText(context, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
            }
    }


public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                    Intent i = new Intent(this, MFLMain.class);
                    if (locationThread != null)
                            if(locationThread.isAlive()){

                    mSensorManager.unregisterListener(senslistener, mAccelerometer);
                    mSensorManager.unregisterListener(senslistener, mGravitometer);
                    mSensorManager.unregisterListener(senslistener);
                    locationThread.interrupt();
                            }
                    locator.stopListener();
                    kill = true;
                    System.out.println("Kill the app "+kill);
                    finish();
                    startActivity(i);
                    return false;
            }
            return super.onKeyDown(keyCode, event);
    }

当我打回隐藏应用程序,杀死活动,我仍然可以在logcat中的System.out的。该THEAD退出,就证明了缺少的System.out的存在。我很迷茫,我不认为范围的问题,因为的SensorManager是通过init方法被实例化和别的地方。

When I hit back to hide the application and kill the activity, I still get the system.out in logcat. The thead quits, as evidenced by the lack of system.out there. I am very confused and I don't think scope is the issue as the sensormanager is being instantiated through an init method and nowhere else .

推荐答案

也许一个长镜头,但我唯一能看到不同于工作过的例子,而你(没有看到额外的code)将实施SensorEventListener并通过这个入册和注销事件,而不是您的自定义SensorEventListener。

maybe a long shot but the only thing I can see different from examples that have worked and yours ( without seeing additional code ) would be to implement the SensorEventListener and pass this into the register and unregister events instead of your custom SensorEventListener.

看到这个计算器的人通过使用new运算符来实例化监听器和这似乎为他工作的答案想你的方式。

see this stackoverflow of someone trying it your way by using the new operator to instantiate the listener and the answer that seemed to work for him.

How转换使用SensorListener来SensorEventListener

public class MySensorActivity extends Activity implements SensorEventListener {


public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        ...
        loopThread();
}

  private void loopThread() {
        boolean found = kill;
        System.out.println("Looping until Found!");
        locationThread = new Thread(){
                private float myacc;

                @Override
                @Override
                public void run() {

                        while (found == false && !kill){
                                System.out.println("Another Loop!");
                                getMyLoc();
                                found = isFound(mylat,mylng,target_lat,target_lng,radius);
                                try {
                                        synchronized(locationThread){
                                                locationThread.wait(100);
                                        }

                                } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                        mSensorManager.unregisterListener(this, mAccelerometer);
                        mSensorManager.unregisterListener(this, mGravitometer);
                        mSensorManager.unregisterListener(this);
                        locationThread.interrupt();
                }
}

@Override
public void onPause(){
        super.onPause();
        mSensorManager.unregisterListener(this, mAccelerometer);
        mSensorManager.unregisterListener(this, mGravitometer);
        locationThread.interrupt();
}
@Override
public void onResume(){
        super.onResume();
        registerSensors();
        mSensorManager.registerListener(this, mAccelerometer,     SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mGravitometer, SensorManager.SENSOR_DELAY_NORMAL);
        if (true)
                loopThread();
}

private void registerSensors() {
        mSensorManager =     (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        List<Sensor> mySensors = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
        if(mySensors.size() > 0){
                mAccelerometer =     mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                mGravitometer =     mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

                mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
                mSensorManager.registerListener(this, mGravitometer, SensorManager.SENSOR_DELAY_NORMAL);
                Toast.makeText(context, "Getting your location...", Toast.LENGTH_LONG).show();
        }
        else{
                Toast.makeText(context, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
        }
}


 public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
                Intent i = new Intent(this, MFLMain.class);
                if (locationThread != null)
                        if(locationThread.isAlive()){

                mSensorManager.unregisterListener(this, mAccelerometer);
                mSensorManager.unregisterListener(this, mGravitometer);
                mSensorManager.unregisterListener(this);
                locationThread.interrupt();
                        }
                locator.stopListener();
                kill = true;
                System.out.println("Kill the app "+kill);
                finish();
                startActivity(i);
                return false;
        }
        return super.onKeyDown(keyCode, event);
}
阅读全文

相关推荐

最新文章