我似乎和这位先生有同样的问题: 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 方法实例化的,而其他地方都没有。