GPS打开后,LocationManager仍然无法找到上次已知的位置 [英] LocationManager still cant find last known location after GPS is turned on

查看:127
本文介绍了GPS打开后,LocationManager仍然无法找到上次已知的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我之前,我已经遇到了一个问题,即使在用户打开位置服务之后,LocationManager仍然无法找到用户的位置。启动需要使用用户位置的活动我称之为:

  //菜单活动
LocationManager lm = (的LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER){
Intent nearby = new Intent(mActivityContext,NearbyActivity.class);
startActivity(nearby);
} else {
alertNoGps();
}

一旦打开GPS,附近的活动可以发布,但LocationManager仍然无法找到用户上次已知的位置。我的相关代码为:

  // // Nearby Activity 
试试{
LocationManager locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!= null){
...
} else {
Toast.makeText ...
}
} catch(SecurityException e){
e.printStackTrace();
}

我目前正在测试的手机正在运行Android Marshmallow 6.0.1。如果GPS在应用程序之前已经打开通过android工作室运行,然后我没有问题得到最后一个已知的位置。只有当我在运行应用程序之前关闭GPS并在应用程序运行时将其打开时,我遇到了问题。 LocationManger导致这种情况怎么办?



编辑:同样在清单中,我有Fine和Coarse位置权限。我不知道这是否是API 23及以上版本的问题,因为需要额外的权限检查,但我必须想象它们仍然由安全异常处理。

解决方案

我发现类似于Pablo的回答,getLastKnownLocation()将被清除,因为它是一个相当随意的函数,可以经常返回null。如果GPS坐标要求更高,那么当您尝试获取GPS坐标并使用requestLocationUpdates()或Google的FusedLocationApi而不是getLastKnownLocation()时,您应该更加有力。

所以现在我使用FusedLocationApi和我使用的GoogleApiClient构建:

  client = new GoogleApiClient.Builder(mActivityContext)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

客户端连接后,我在onConnected()方法覆盖中找到GPS位置,如下所示:

  LocationManager locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
位置位置= LocationServices.FusedLocationApi.getLastLocation(client);

在寻找GPS坐标系时,这似乎更可靠。


I'm having a problem with android where even after the user turns their location service on, the LocationManager still can't find the users location.

Before I launch the activity that needs to use the user's location I call the following:

//Menu Activity
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER){
    Intent nearby = new Intent(mActivityContext, NearbyActivity.class);
    startActivity(nearby);
}else{
    alertNoGps();
}

Once GPS is turned on the nearby activity can be launched, but the LocationManager still cannot find the users last known location. My relevant code for this is:

//Nearby Activity
try{
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(location != null){
        ...
    }else{
        Toast.makeText ...
    }
} catch(SecurityException e){
    e.printStackTrace();
}

The phone I'm currently testing on is running Android Marshmallow 6.0.1. If the GPS is already turned on before the app is run through android studio then I have no problem getting the last known location. It's only when the GPS is turned off before I run the app and turn it on while the app is running that I have problems. What about LocationManger is causing this?

Edit: Also in the manifest I have both Fine and Coarse location permissions. I do not know if this is an issue with API 23 and above because of the extra permission checks required, but I have to imagine they are still handled by the Security Exception.

解决方案

I found out that similar to Pablo's answer, getLastKnownLocation() will be cleared and because it is a rather casual function can return null quite often. If GPS coordinates are more demanded, then you should be more forceful when trying to get GPS coordinates and use something like requestLocationUpdates() or Google's FusedLocationApi rather than getLastKnownLocation().

So now I am using FusedLocationApi with a GoogleApiClient I built using:

client = new GoogleApiClient.Builder(mActivityContext) 
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

After the client is connected I find the GPS location in the onConnected() method override which looks like:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = LocationServices.FusedLocationApi.getLastLocation(client);

This seems to be much more reliable in finding GPS coordinates.

这篇关于GPS打开后,LocationManager仍然无法找到上次已知的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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