【问题标题】:RemoveUpdates of LocationListener is not working on service classLocationListener 的 RemoveUpdates 不适用于服务类
【发布时间】:2016-03-26 05:37:54
【问题描述】:

我正在尝试在获取位置后删除 locationListener。我正在使用与位置更新请求相同的 listenerOBj,但它不起作用。我还有一个问题是调用 serviceClassObject.stopself 不会停止服务?

提前致谢。

这是我的代码

public class GPSTracker extends Service implements LocationListener {


private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManagerGPs,locationManagerNet;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManagerGPs = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);
        locationManagerNet = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManagerGPs
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManagerNet
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        locationManagerGPs.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MIN_TIME_BW_UPDATES,
                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

        locationManagerNet.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,
                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

         if (isGPSEnabled || isNetworkEnabled) {
            this.canGetLocation = true;
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {

                if (locationManagerGPs != null) {
                    location = locationManagerGPs
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();

                        Log.d("tttt","GPStracker taken complete");
                    }
                }
            }
            else{

                if (locationManagerNet != null) {
                    location = locationManagerNet
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();

                        Log.d("tttt","network taken complete");
                    }
                }
            }
        }else{
             this.canGetLocation = false;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }
    return longitude;
}
public void stopUsingGPS(){
    if(locationManagerGPs != null){
        Log.d("tttt","trying to stop GPStracker");
        locationManagerGPs.removeUpdates(this);
    }
    if(locationManagerNet != null){
        Log.d("tttt","trying to stop GPStracker net");
        locationManagerNet.removeUpdates(this);
    }
}

public boolean canGetLocation() {
    return this.canGetLocation;
}


@Override
public void onLocationChanged(Location location) {
    Log.d("tttt","onLocationChanged called on GPStracker");
    getLocation();
}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {
    Log.d("tttt","location provider is enabled");
    getLocation();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("tttt","onStatusChanged called on GPStracker");
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

我从活动中调用

GPSTracker gps;
gps = new GPSTracker(HomeActivity.this);

if(gps.canGetLocation()){
lat = gps.getLatitude();
lng = gps.getLongitude();

gps.stopUsingGPS();
gps.stopSelf();
}

【问题讨论】:

    标签: android service gps location locationlistener


    【解决方案1】:

    最后,我终于得到了解决方案。主要问题在于使用服务的方法。仅仅通过扩展服务类并不是使用服务的正确方式。当我们需要在服务的帮助下完成某些任务时,必须遵循一些步骤。

    第 1 步:扩展服务类后,我们需要重写 onStartCommand(Intent intent, int flags, int startId) 和 onDestroy() 方法。

    第 2 步:将其添加到清单中,如

    第 3 步:启动服务写入 startService(new Intent(getBaseContext(), className.class));

    第 4 步:任务完成后将其停止为 stopService(new Intent(getBaseContext(), className.class));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2013-12-11
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      相关资源
      最近更新 更多