几分钟后“放弃”GPS -Android

时间:2011-07-05 18:41:24

标签: android gps location

我正在尝试在我的应用上设置位置服务。

我想在没有GPS信号的情况下向用户显示错误(在尝试寻找信号X分钟后)。

我该怎么做?

到目前为止,这是我的代码 -

    // Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
            Log.d("loca", "accuracy - "  + location.getAccuracy());
            Log.d("loca", "longtitude - "  + location.getLongitude());
            Log.d("loca", "Latitude - "  + location.getLatitude());
            Log.d("loca", "Provider - "  + location.getProvider());
            if(isBetterLocation(location,bestLoc))
            {
                Log.d("loca", "best loc - yes");
                bestLoc = location;
            }
            else
                Log.d("loca", "best loc - no");


        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
      };



    // Register the listener with the Location Manager to receive location updates
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
    {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,TWO_MINUTES , 300, locationListener);
        Log.d("loca", "GPS");
    }
    else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
    {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,TWO_MINUTES , 300, locationListener);
            Log.d("loca", "Net");
    }
    else
        Log.d("loca", "Nothing");

我怎么知道自从我开始寻找信号以来已经有多长时间了?

谢谢!

1 个答案:

答案 0 :(得分:2)

Service使用CountDownTimer。开始服务时开始请求位置更新。当您开始请求更新时,启动具有定义持续时间的CountDownTimer(在下面的示例中为15秒)。如果找到位置,请检查每个倒计时滴答。在倒数计时器结束时(onFinish),如果找不到位置,请使用stopSelf停止服务并快速检查最后一个已知位置。

示例代码片段让您入门:

//start requesting updates...
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    startTimer();
}
else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    startTimer();
}
else {
    //do nothing since nothing is enabled
    stopSelf();
}

//function to start the timer once you start requesting updates
private void startTimer() {
    if(countDownTimer != null) {
        countDownTimer.cancel();
        countDownTimer = null;
    }
    countDownTimer = new CountDownTimer(15 * 1000L, 1000L) {//15 seconds max
        @Override
        public void onTick(long millisUntilFinished) {
            if(didFindLocation) {
                cancel();
            }
        }
        @Override
        public void onFinish() {
            if(!didFindLocation) {
                stopSelf();
            }
        }
    };
    countDownTimer.start();
}

//when your service stops
@Override
public void onDestroy() {
    if(!didFindLocation) {
        Location location = null;
        if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }
        if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) && location == null) {
            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
        //do something with location if not null
    }
    locationManager.removeUpdates(this);
    if(countDownTimer != null) {
        countDownTimer.cancel();
        countDownTimer = null;
    }
    super.onDestroy();
}

@Override
public void onLocationChanged(Location location) {
    didFindLocation = true;
    //do something with location
}