【问题标题】:Is it better to call LocationManager once and keep in memory, or only call it when needed?调用 LocationManager 一次并保存在内存中,还是只在需要时调用它更好?
【发布时间】:2012-08-13 13:27:05
【问题描述】:

这是一个与 LocationManager 相关的效率问题(更一般地,与管理内存与 Android 中的 CPU 使用率有关)。假设我有一个长时间运行的服务,它希望每 60 秒使用一次 LocationManager 的 getLastKnownLocation 方法来更新位置。该服务使用一个 TimerTask 和一个 Time 重复固定延迟执行。是创建一个实例字段 mLocationManager 并在服务的生命周期内保留它更好,还是在 TimerTask 的每次执行时实例化 LocationManager 更好,据说 VM 只会在需要时保留它?在代码中:

public class ProximityService extends Service {

    private LocationManager mLocationManager;

    @Override
    public void onCreate() {
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Timer timer = new Timer();
        TimerTask mGetLastKnownLocationTask = new GetLastKnownLocationTask();
        timer.schedule(mGetLastKnownLocationTask, 0, 60000);
    }

    private class GetLastKnownLocationTask extends TimerTask {

       public void run() {
           Location mLocation = 
               mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
               // Do something with mLocation
       }
    }
...
}

对比

...

@Override
public void onCreate() {
    Timer timer = new Timer();
    TimerTask mGetLastKnownLocationTask = new GetLastKnownLocationTask();
    timer.schedule(mGetLastKnownLocationTask, 0, 60000);
}

private class GetLastKnownLocationTask extends TimerTask {

   public void run() {
       LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       Location mLocation = 
           mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
           // Do something with mLocation
   }
}

注意:我不需要 LocationListener 来保持 GPS 处于活动状态。这是在应用程序的另一部分使用单独的服务处理的。这里我只想以固定的时间间隔检查最近的已知位置。

【问题讨论】:

    标签: android optimization memory-management locationmanager


    【解决方案1】:

    获取位置管理器一次,然后注册一个被动位置侦听器不是更好吗?当您没有时间获得有效的位置修复时,必须调用 getLastKnownLocation() 应该是一种快速的解决方案,不一定是为了重复使用。

    喜欢...

        LocationManager lm = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);
        //change best provider to a passive location service.
        String bestProvider = lm.getBestProvider(new Criteria(), true);
        if(bestProvider!=null){
           lm.requestLocationUpdates(bestProvider, 1000 * 60 * 15 ,5000, 
           new LocationListener {
    
            public void onLocationChanged(Location location) {
    
            }
    
            public void onProviderDisabled(String provider) {}
    
            public void onProviderEnabled(String provider) {}
    
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            }
            );
    
        }
    

    【讨论】:

    • 感谢您的回答,但我使用 getLastKnownLocation() 而不是 LocationListener 有几个原因。首先是我想确保我的更新能够启动,无论 GPS 是否可用。例如,如果用户已经在室内一段时间,那么我假设他/她的最佳位置是使用 getLastKnownLocation() 获得的位置。 getLastKnownLocation() 不应该以重复的方式使用是否有任何技术原因,或者只是您的印象,它不打算以这种方式使用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 2014-02-07
    • 2011-01-20
    • 2015-07-26
    • 1970-01-01
    相关资源
    最近更新 更多