栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android,线程,无法注销SensorEventListener

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android,线程,无法注销SensorEventListener

也许是一个长镜头,但我能看到的与已完成的示例以及您的示例(没有看到其他代码)不同的唯一地方是实现SensorEventListener并将其传递给注册和注销事件,而不是自定义的SensorEventListener。

看到有人通过使用new运算符实例化侦听器和似乎对他有用的答案来尝试这种方式的堆栈溢出。

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();     }}@Overridepublic void onPause(){        super.onPause();        mSensorManager.unregisterListener(this, mAccelerometer);        mSensorManager.unregisterListener(this, mGravitometer);        locationThread.interrupt();}@Overridepublic 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);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/515985.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号