经过近GPS不更新位置,并在Android应用程序重新打开(GPS not update locat

2019-07-05 11:02发布

我闭上我的应用程序后一会儿然后再重新打开它,我的应用程序将不会更新位置或某个时候将需要很长一段时间(约5分钟)更新之前。 我怎样才能解决这个问题? 这是我的代码

  private LocationManager lm;
private LocationListener locationListener;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);    

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

    locationListener = new mLocationListener();

    lm.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 0, 0, locationListener); }


private class myLocationListener implements LocationListener 
{
    @Override
    public void onLocationChanged(Location loc) {
        if (loc != null) {
            TextView gpsloc = (TextView) findViewById(R.id.widget28);
            gpsloc.setText("Lat:"+loc.getLatitude()+" Lng:"+ loc.getLongitude());
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        TextView gpsloc = (TextView) findViewById(R.id.widget28);
        gpsloc.setText("GPS OFFLINE.");
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, 
        Bundle extras) {
        // TODO Auto-generated method stub
    }
}

Answer 1:

一些东西...

什么是可能发生的事情是你的应用程序正在恢复,而不是创作的“第二”的时间。 如果你想让它每次活动被视为时间寻找一个新的位置,你会想下面的代码移动到onResume()方法:

lm.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 0, 0, locationListener); }

您还需要确保您注销您的LocationListener的中onPause()方法。 像这样的东西应该这样做:

@Override
public void onPause() {
    lm.removeUpdates(locationListener);
    super.onPause();
}

它看起来像你必须在这条线一个错字:

locationListener = new mLocationListener();

我觉得应该是:

locationListener = new myLocationListener();

此外,如别人所说,没有任何理由来创造一个全新的类作为您LocationListener的。 事实上,它会不必要地与移动设备上的内存是非常珍贵的烧起来的记忆。

所有的东西的结合上面会留下你的活动看起来像这样:

public class Whatever extends Activity implements LocationListener {
    private LocationManager lm;
    private LocationListener locationListener;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);    

        lm = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);    
        locationListener = new mLocationListener();
    }

    @Override 
    public void onResume() {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
        super.onResume();
    }

    @Override
    public void onPause() {
        lm.removeUpdates(this);
        super.onPause();
    }

    @Override
    public void onLocationChanged(Location loc) {
        if (loc != null) {
            TextView gpsloc = (TextView) findViewById(R.id.widget28);
            gpsloc.setText("Lat:"+loc.getLatitude()+" Lng:"+ loc.getLongitude());
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        TextView gpsloc = (TextView) findViewById(R.id.widget28);
        gpsloc.setText("GPS OFFLINE.");
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }
}


Answer 2:

为什么ü使用了0 mintime和0精度如果没有必要就非常fast..avoid耗尽你的电池这第一件事
其次ü这里提到的时间滞后是绝对访问其可以变化有时取决于GPS信号的可用性GPS数据由GPS芯片所用的时间
以及关于更新ü应该试试这个

public void onLocationChanged(Location loc) {
        if (loc != null) {
            TextView gpsloc = (TextView) findViewById(R.id.widget28);
            gpsloc.setText("Lat:"+loc.getLatitude()+" Lng:"+ loc.getLongitude());
        }
        else
             {gpsloc.setText("provider not available");}//provider available or not 

    }

有可能是一个机会,没有可用的更新位置提供



文章来源: GPS not update location after close and reopen app on android
标签: android gps