locationManager.getLastKnownLocation(locationProvider);//地理位置获取为null的解决方案
方案一:没有加权限
【locationManager.getLastKnownLocation(locationProvider); //地理位置获取为null的解决方案】方案二:(完整代码)
public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";
private LocationManager locationManager;
private String locationProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);
//下面注释的代码获取的location为null,所以采用Criteria的方式。
/*List providers = locationManager.getProviders(true);
if (providers.contains(LocationManager.GPS_PROVIDER)) {
//如果是GPS
locationProvider = LocationManager.GPS_PROVIDER;
Log.d(TAG, "onCreate: gps=" + locationProvider);
} else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
//如果是Network
locationProvider = LocationManager.NETWORK_PROVIDER;
Log.d(TAG, "onCreate: network=" + locationProvider);
} else {
Log.d(TAG, "onCreate: 没有可用的位置提供器");
Toast.makeText(this,"没有可用的位置提供器",Toast.LENGTH_SHORT).show();
return;
}
//获取Location,老是获取为空!所以用locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(locationProvider);
*/
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
//低精度,如果设置为高精度,依然获取不了location。
criteria.setAltitudeRequired(false);
//不要求海拔
criteria.setBearingRequired(false);
//不要求方位
criteria.setCostAllowed(true);
//允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);
//低功耗//从可用的位置提供器中,匹配以上标准的最佳提供器
locationProvider = locationManager.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "onCreate: 没有权限 ");
return;
}
Location location = locationManager.getLastKnownLocation(locationProvider);
Log.d(TAG, "onCreate: " + (location == null) + "..");
if (location != null) {
Log.d(TAG, "onCreate: location");
//不为空,显示地理位置经纬度
showLocation(location);
}
//监视地理位置变化
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
}/**
* LocationListern监听器
* 参数:地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器
*/LocationListener locationListener = new LocationListener() {@Override
public void onStatusChanged(String provider, int status, Bundle arg2) {}@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled: " + provider + ".." + Thread.currentThread().getName());
}@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "onProviderDisabled: " + provider + ".." + Thread.currentThread().getName());
}@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged: " + ".." + Thread.currentThread().getName());
//如果位置发生变化,重新显示
showLocation(location);
}
};
private void showLocation(Location location) {
Log.d(TAG,"定位成功------->"+"location------>经度为:" + location.getLatitude() + "\n纬度为" + location.getLongitude());
}
}