「放弃」几分钟后打开 GPS -Android

发布于 2024-11-18 11:49:46 字数 1955 浏览 1 评论 0原文

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

我想在没有可用 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");

我如何知道自从我开始寻找信号以来已经过去了多长时间?

谢谢!

I'm trying to set up location services on my app.

I'd like to show the user an error in a case where there is no GPS signal available(After X minutes of trying to find a signal).

How can I do that?

Here's my code so far -

    // 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");

How can I know how long it has been since I've started looking for a signal?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反差帅 2024-11-25 11:49:46

ServiceCountDownTimer 结合使用。当您启动服务时,开始请求位置更新。当您开始请求更新时,启动一个具有定义的持续时间(在下面的示例中为 15 秒)的 CountDownTimer。检查每个倒计时刻度是否找到位置。在倒计时器 (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
}

Use a Service with a CountDownTimer. Begin requesting location updates when you start your service. right when you start requesting updates, start a CountDownTimer with a defined duration(in the example below, 15 seconds). check on every countdown tick if a location was found. at the end of the countdowntimer(onFinish), if no location was found stop the service using stopSelf and quickly check for a last known location.

Sample code snippets to get you started:

//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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文