1。 添加权限 AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
@SuppressLint("MissingPermission")
public void refreshLocation() {
logger.d("refreshLocation() 000");
ThreadExecutor.getInstance().schedule(new Runnable() {
@Override
public void run() {
if (isViewClose()) { return; }
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
mLocationProvider = LocationManager.NETWORK_PROVIDER;
} else if (providers.contains(LocationManager.GPS_PROVIDER)) {
mLocationProvider = LocationManager.GPS_PROVIDER;
} else {
if (!isGpsAble(mLocationManager)) {
ToastUtils.showShort("请打开GPS~");
openGPS2();
}
return;
}
Location location = mLocationManager.getLastKnownLocation(mLocationProvider);
if (location != null) {
final double latitude = location.getLatitude();
final double longitude = location.getLongitude();
logger.d("refreshLocation latitude {} , longitude {} " + latitude + " longitude : " + longitude);
ToastUtils.showShort("经度:" + longitude + "\n纬度:" + latitude);
// getAddress(latitude, longitude);
//监听位置变化,2秒一次,距离1米以上
mLocationManager.requestLocationUpdates(mLocationProvider, 2 * 1000, 100, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
logger.d("refreshLocation latitude {} , longitude {} " + location.getLatitude() + " longitude : " + location.getLongitude());
ToastUtils.showShort("经度:" + location.getLongitude() + "\n纬度:" + location.getLatitude());
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
logger.d(provider + "---" + status + "===" + status + extras.toString());
}
@Override
public void onProviderEnabled(String provider) {
logger.d(provider + "---");
}
@Override
public void onProviderDisabled(String provider) {
logger.d(provider + "--777-");
}
});
} else {
}
}
}, 2000);
}
private boolean isGpsAble(LocationManager lm) {
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ? true : false;
}
//打开设置页面让用户自己设置
private void openGPS2() {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, AppConstant.ActivityRequestCode.ACCOUNT_KIT_LOGIN);
}
public void getAddress(double latitude, double longitude) {
if (isViewClose()) { return; }
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
if (address != null) {
String data = address.toString();
if (TextUtils.isEmpty(data)) {
LogUtils.d("getAddress() (TextUtils.isEmpty(data)");
// updateUserInfo(latitude, longitude, null);
} else {
int startCity = data.indexOf("admin=") + "admin=".length();
int endCity = data.indexOf(",", startCity);
int startPlace = data.indexOf("thoroughfare=") + "thoroughfare=".length();
int endPlace = data.indexOf(",", startPlace);
int startCountry = data.indexOf("countryName=") + "countryName=".length();
int endCountry = data.indexOf(",", startCountry);
String country = data.substring(startCountry, endCountry);
String city = data.substring(startCity, endCity);
String place = data.substring(startPlace, endPlace);
// updateUserInfo(latitude, longitude, country + "\n" + city + "\n" + place);
logger.d("getAddress()" + country + "\n" + city + "\n" + place);
// ToastUtils.showShort("country:" + country + "\ncity:" + city + "\nplace:" + place);
}
} else {
// updateUserInfo(latitude, longitude, null);
}
} else {
// updateUserInfo(latitude, longitude, null);
}
} catch (IOException e) {
e.printStackTrace();
// updateUserInfo(latitude, longitude, null);
}
}
public boolean isViewClose() {
return (ActivityUtil.isFinishing(this));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AppConstant.ActivityRequestCode.ACCOUNT_KIT_LOGIN) {
refreshLocation();
}
}
}
1242

被折叠的 条评论
为什么被折叠?



