【发布时间】:2016-12-16 18:09:31
【问题描述】:
好吧,这很奇怪,但我可能遗漏了一些显而易见的东西。我目前正在使用此功能访问用户位置:
public String[] getGeoCoords() {
String[] geoCoords = new String[2];
String mprovider = locationManager.getBestProvider(criteria, false);
if (mprovider != null && !mprovider.equals("")) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return geoCoords;
}
Location loc = locationManager.getLastKnownLocation(mprovider);
if(loc== null)
loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc == null)
loc = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (loc == null)
loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
geoCoords[0] = String.valueOf(loc.getLatitude());
geoCoords[1] = String.valueOf(loc.getLongitude());
}
return geoCoords;
}
这会按预期返回位置,但您通常在访问用户位置时获得的图标不会出现在状态栏中。
在我的清单中
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
有什么想法吗?
【问题讨论】:
-
尝试添加这个权限
-
在我的情况下,我总是将 Android 图标作为状态栏。是你面对的吗?
标签: android location-services android-gps