我创建了一个服务,用于获取我从主线程调用的用户位置。现在,这项服务不仅仅来自gps,还来自网络。为什么它会在某些手机和某些手机上获得用户位置呢?
这是我的服务类:
import android.app.Service;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import java.util.List;
public class LocationService extends Service implements LocationListener {
protected LocationManager locationManager;
public String providerNow;
private List<String> providers;
public Location location;
public LocationService() {
}
@Override
public void onCreate() {
super.onCreate();
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
providerNow = (GetProvider());
}
public Location getLocation() {
location = locationManager.getLastKnownLocation(providerNow);
return location;
}
public String GetProvider() {
Criteria criteria = new Criteria();
criteria.setAccuracy(2);
providers = locationManager.getProviders(true);
String providerToSend = providers.get(1);
return providerToSend;
}
public IBinder onBind(Intent intent) {
Log.i("TestMap", "onBind");
//TODO for communication return IBinder implementation
return null;
}
public void onLocationChanged(Location location) {
Static.location = location;
if (location != null) {
Log.d("Lat", String.valueOf(location.getLatitude()));
Log.d("Lon", String.valueOf(location.getLongitude()));
Static.latitude = location.getLatitude();
Static.longitude = location.getLongitude();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
providerNow = (GetProvider());
}
@Override
public void onProviderDisabled(String provider) {
providerNow = (GetProvider());
}
}
以下是我正在调用此服务的主线程中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Intent serviceIntent = new Intent(this, LocationService.class);
startService(serviceIntent);
proveraInterneta();
getSizeOfTheScreen();
}
答案 0 :(得分:2)
以下是您面临的问题的两个建议:
根据编码,请使用Google的 Fused Location API 。可以通过LocationServices访问FusedLocationProviderApi,如下所示,
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
FusedLocationProviderApi需要GoogleApiClient实例获取位置,并且可以通过以下方式获取。
googleApiClient = new GoogleApiClient.Builder(locationActivity)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
最后但并非最不重要,请检查清单中是否添加了此权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
请查看此tutorial以获取更多参考资料。
答案 1 :(得分:0)
据我了解您的代码,当您调用getLocation方法时,从当前工作提供程序(或最新启用的提供程序)获取最新位置。启用提供程序时,无法保证从getLocation方法获取位置。
如果我在启用gps提供程序后立即调用getLastKnownLocation(“gps”),那么正常获取null结果。因为gps提供者必须至少一次触发onLocationChanged方法,所以我必须等待gps修复。此场景也适用于网络提供商。
调用getLastKnownLocation(“提供者”)方法而不确定提供程序是否已修复,将为您提供不可靠(旧位置)或null结果。在这种情况下,如果某些手机没有给出null,则意味着这些设备已经为当前提供商缓存了位置,或者他们很幸运,因为提供商已经预热了