「放弃」几分钟后打开 GPS -Android
我正在尝试在我的应用程序上设置位置服务。
我想在没有可用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
Service
与CountDownTimer
结合使用。当您启动服务时,开始请求位置更新。当您开始请求更新时,启动一个具有定义的持续时间(在下面的示例中为 15 秒)的CountDownTimer
。检查每个倒计时刻度是否找到位置。在倒计时器 (onFinish
) 结束时,如果未找到位置,请使用stopSelf
停止服务并快速检查最后一个已知位置。帮助您入门的示例代码片段:
Use a
Service
with aCountDownTimer
. Begin requesting location updates when you start your service. right when you start requesting updates, start aCountDownTimer
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 usingstopSelf
and quickly check for a last known location.Sample code snippets to get you started: