public class MainActivity extendsAppCompatActivity {private static final String TAG = "MainActivity";privateLocationManager locationManager;privateString locationProvider;
@Overrideprotected voidonCreate(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= newCriteria();
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= newLocationListener() {
@Overridepublic void onStatusChanged(String provider, intstatus, Bundle arg2) {
}
@Overridepublic voidonProviderEnabled(String provider) {
Log.d(TAG,"onProviderEnabled: " + provider + ".." +Thread.currentThread().getName());
}
@Overridepublic voidonProviderDisabled(String provider) {
Log.d(TAG,"onProviderDisabled: " + provider + ".." +Thread.currentThread().getName());
}
@Overridepublic voidonLocationChanged(Location location) {
Log.d(TAG,"onLocationChanged: " + ".." +Thread.currentThread().getName());//如果位置发生变化,重新显示
showLocation(location);
}
};private voidshowLocation(Location location) {
Log.d(TAG,"定位成功------->"+"location------>经度为:" + location.getLatitude() + "
纬度为" +location.getLongitude());
}
}