gpt4 book ai didi

java - Android,Thread,无法取消注册 SensorEventListener

转载 作者:太空狗 更新时间:2023-10-29 14:21:32 26 4
gpt4 key购买 nike

我好像和这位先生有同样的问题: 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);
}

当我回击以隐藏应用程序并终止 Activity 时,我仍然在 logcat 中得到 system.out。 thead 退出,正如缺少 system.out 所证明的那样。我很困惑,我不认为范围是问题,因为传感器管理器是通过 init 方法而不是其他地方实例化的。

最佳答案

也许是一个远景,但我能看到的与已经运行的示例和您的示例(没有看到额外的代码)唯一不同的是实现 SensorEventListener 并将其传递到注册和注销事件中,而不是您的自定义 SensorEventListener。

请看这个 stackoverflow,有人尝试按照您的方式使用 new 运算符来实例化监听器和似乎对他有用的答案。

How to convert a program that uses SensorListener to 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);
}

关于java - Android,Thread,无法取消注册 SensorEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16504474/

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