本文主要是介绍locationManager.getLastKnownLocation(locationProvider);//地理位置获取为null的解决方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
方案一:没有加权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><uses-permission android:name="android.permission.INTERNET"/>
方案二:(完整代码)
public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";private LocationManager locationManager;private String locationProvider;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);locationManager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);//下面注释的代码获取的location为null,所以采用Criteria的方式。/*List<String> providers = locationManager.getProviders(true);if (providers.contains(LocationManager.GPS_PROVIDER)) {//如果是GPSlocationProvider = LocationManager.GPS_PROVIDER;Log.d(TAG, "onCreate: gps=" + locationProvider);} else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {//如果是NetworklocationProvider = 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() {@Overridepublic void onStatusChanged(String provider, int status, Bundle arg2) {}@Overridepublic void onProviderEnabled(String provider) {Log.d(TAG, "onProviderEnabled: " + provider + ".." + Thread.currentThread().getName());}@Overridepublic void onProviderDisabled(String provider) {Log.d(TAG, "onProviderDisabled: " + provider + ".." + Thread.currentThread().getName());}@Overridepublic 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());}
}
这篇关于locationManager.getLastKnownLocation(locationProvider);//地理位置获取为null的解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!