全球定位系统关闭后不更新位置,并重新对Android应用程序 [英] GPS not update location after close and reopen app on android

查看:211
本文介绍了全球定位系统关闭后不更新位置,并重新对Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我结束我的应用程序了一会儿,然后再重新打开它,我的应用程序将不会更新位置或 有时它会更新前需要很长时间(约5分钟)。 我该如何解决这个问题?这是我的code

 私人LocationManager流明;
私人LocationListener的LocationListener的;

  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

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

    LocationListener的=新mLocationListener();

    lm.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,0,0,LocationListener的); }


私有类myLocationListener实现LocationListener的
{
    @覆盖
    公共无效onLocationChanged(位置LOC){
        如果(LOC!= NULL){
            TextView的gpsloc =(TextView中)findViewById(R.id.widget28);
            gpsloc.setText(纬度:+ loc.getLatitude()+LNG:+ loc.getLongitude());
        }
    }

    @覆盖
    公共无效onProviderDisabled(字符串提供商){
        // TODO自动生成方法存根
        TextView的gpsloc =(TextView中)findViewById(R.id.widget28);
        gpsloc.setText(GPS离线。);
    }

    @覆盖
    公共无效onProviderEnabled(字符串提供商){
        // TODO自动生成方法存根
    }

    @覆盖
    公共无效onStatusChanged(字符串商,INT地位,
        捆绑演员){
        // TODO自动生成方法存根
    }
}
 

解决方案

有几件事情...

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

  lm.requestLocationUpdates(
    LocationManager.GPS_PROVIDER,0,0,LocationListener的); }
 

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

  @覆盖
公共无效的onPause(){
    lm.removeUpdates(LocationListener的);
    super.onPause();
}
 

看起来你对这个行一个错字:

  LocationListener的=新mLocationListener();
 

我想应该是:

  LocationListener的=新myLocationListener();
 

此外,如别人所说,有没有必要创建一个全新的类作为您LocationListener的。事实上,它会燃烧起来不必要的内存和移动设备内存在premium。

所有的东西的结合上面会离开你的Activity看起来像这样:

  

After I closed my app for a while then reopen it again,my app will not update location or sometime it will take long time( about 5min) before update. How can I fix it? This is my code

  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
    }
}

解决方案

A few things...

What's probably happening is your app is being resumed and not created the "second" time. If you want it to look for a new location every time the activity is viewed you'll want to move the following code into the onResume() method:

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

You'll also want to make sure you unregister your LocationListener in the onPause() method. Something like this should do it:

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

It looks like you have a typo on this line:

locationListener = new mLocationListener();

I think it should be:

locationListener = new myLocationListener();

Also, as someone else mentioned, there's no reason to create a whole new class to act as your LocationListener. In fact, it'll burn up memory unnecessarily and on mobile devices memory is at a premium.

The combination of all of the things above would leave your Activity looking something like this:

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
    }
}

这篇关于全球定位系统关闭后不更新位置,并重新对Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆