【问题标题】:getLastKnownLocation() returns null ; it was working a day agogetLastKnownLocation() 返回 null ;一天前还在工作
【发布时间】:2014-04-04 23:37:06
【问题描述】:

getLastKnownLocation 现在返回 null 但它在一天前完全正常工作!现在它始终为空,来自不同的手机。

可能是什么问题?我是否以错误的方式实施它? locationFetch 线程不断运行 - 内部实现了一个 while 循环 - 并负责在每次更改时调用“onLocationChanged”函数。

Runnable locationFetchRun = new Runnable() 
        {
            public void run() 

            {       

                tracker = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                Criteria criteria = new Criteria();
                Location location;
                boolean enabled;

                // Initialize the location fields
                while( !isFinishing() )
                {
                    Log.i("Homepage","User id value "+user_id);

                     tracker = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                      enabled = tracker
                      .isProviderEnabled(LocationManager.GPS_PROVIDER);

                     connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                     activeNetworkInfo = connectivityManager.getActiveNetworkInfo();


                     //Brick point

                      if(!enabled || activeNetworkInfo.isConnected()!= true)
                      {
                          continue;
                      }

                provider = tracker.getBestProvider(criteria, true);
                location = tracker.getLastKnownLocation(provider);



                if (location != null) 
                {   

                  gps_state = 1;
                  onLocationChanged(location);
                } 

                else
                gps_state = 0;




                }

            }
      };

      //Start location fetch thread
    Thread locationFetch = new Thread(locationFetchRun);
    locationFetch.start();

【问题讨论】:

  • 尝试重新启动设备并确保在设置中启用位置。如果一切顺利,我建议使用 LocationClient 获取当前位置
  • 感谢 Libin 的建议,它实际上刚刚工作,但经过一个小时左右的更新......这使得线程杀死了我手机的电池。我会搜索 locationClient 和它的用法。

标签: java android


【解决方案1】:

它实际上并没有停止工作,只是花了很多时间来获取坐标。我会寻找另一种解决方案来节省时间和手机电池;从更改整个函数开始,到在循环结束时设置一个合适的超时。

【讨论】:

    猜你喜欢
    • 2013-11-06
    • 2013-12-24
    • 2012-05-28
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    相关资源
    最近更新 更多