前些天为app添加了获取地理位置的功能,当时是在发表内容的页面发起获取位置请求,今天又要为评论也添加位置,所以就把获取请求放在了Home页,在每次App启动的时候都获取一下,避免在多个位置重复获取。
1、发起请求
HomeActivity.class
postDelay(new Runnable() {
@Override
public void run() {
requestLocation();
}}, 2000);```
HomeActivity.class
private void requestLocation() {
//for android M
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_GRANTED_LOCATION);
} else {
requestLocation0();
}
}
HomeActivity.class
//for android M, Refer: https://www.aswifter.com/2015/11/04/android-6-permission/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_GRANTED_LOCATION) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
requestLocation0();
} else {
L.e(TAG, "granted failed!");
}
}
}
private void requestLocation0() {
Location location = LocationUtil.getLocation(this);
LocationUtil.removeListener(this);
//立即关闭,否则定位图标会一直显示在状态栏(华为、三星)
if (location == null) {
return;
}
String loc = location.getLatitude() + "," + location.getLongitude();
//请求数据
http.goBackground(Api.get().getAddress(Consts.BAIDU_GEO_AK, loc, "json"),
new HttpSuccessCallback<ApiData.GeoAddressData>() {
@Override
public void onSuccess(Call<ApiData.GeoAddressData> call, Response<ApiData.GeoAddressData> response) {
String address = getSimplyAddress(response.body());
ApiTools.getInstance().saveUserAddress(address);//把结果保存起来,在需要用的时候直接从SP中get即可
}
});
}
2、通过百度获取位置
@GET("http://api.map.baidu.com/geocoder/v2/")//?ak=appKey&location=latitude,longitude&output=json
Call<ApiData.GeoAddressData> getAddress(@Query("ak") String appKey, @Query("location") String location, @Query("output")String output);
3、获取Location 的帮助类
LocationUtil.class
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;import android.util.Log;
/** * Created by Administrator on 2015/12/26. */
public class LocationUtil {
private static LocationManager mLocationManger;
public static Location getLocation(Context context) {
Location location = null;
boolean isGPSEnabled;
boolean isNetworkEnabled;
mLocationManger = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = mLocationManger.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = mLocationManger.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled || isNetworkEnabled) {
if (isNetworkEnabled) {
mLocationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Log.d("Network", "Network Enabled");
if (mLocationManger != null) {
location = mLocationManger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
if (isGPSEnabled) {
if (location == null) {
mLocationManger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Log.d("Network", "Network Enabled");
if (mLocationManger != null) {
location = mLocationManger.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
}
return location;
}
static LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle arg2) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onLocationChanged(Location location) {
//如果位置发生变化,重新显示
} };
public static String getAddress(ApiData.GeoAddressData addressResult) {
if (addressResult == null)
return "";
ApiData.GeoAddressData.UserAddress.AddressComponent addressComponent = addressResult.result.addressComponent;
if (addressComponent.province.equals(addressComponent.city)) {
return addressComponent.city + " " + addressComponent.district;
} else {
return addressComponent.province + " " + addressComponent.city + " " + addressComponent.district;
}
}
public static void removeListener(Context context) {
if (mLocationManger != null) {
//移除监听器
mLocationManger.removeUpdates(locationListener);
}
}
}