1.AndroidMainfext.xml添加权限
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
2.AndroidMainfext.xml 加入android:targetSdkVersion=”23″ (为什么要加入这个是为了配合安卓6.0以上系统需要动态申请权限,加入这个不写或者低于23则23以及以上的会无法获得权限)
3.获取定位服务以及当前可用的控制器
private static LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if(location != null){
String string = "纬度为:" + location.getLatitude() + ",经度为:"+ location.getLongitude();
Log.i(logTag,"string"+string);
AndroidLocation.getAddress(location);
AndroidLocation.onActivityStoped();
}
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
public static void startLocation(Context context){
mContext = context;
//获取定位服务
m_locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//获取当前可用的位置控制器
List<String> list = m_locationManager.getProviders(true);
if (list.contains(LocationManager.GPS_PROVIDER)) {
//是否为GPS位置控制器
m_provider = LocationManager.NETWORK_PROVIDER;//NETWORK_PROVIDER GPS_PROVIDER
Log.i(logTag,"is GPS");
}
else if (list.contains(LocationManager.NETWORK_PROVIDER)) {
//是否为网络位置控制器
m_provider = LocationManager.NETWORK_PROVIDER;
Log.i(logTag,"is network");
}
if(m_provider != null){
Location location = m_locationManager.getLastKnownLocation(m_provider);
if(location!=null){
//AndroidLocation.getAddress(location);
//直接获取
}else{
//没有需要加监听等待获取
m_locationManager.requestLocationUpdates(m_provider, 3000, 1, locationListener);
}
}
}
4.在activity的生命周期内要记得删除listener,我的需求是获取到地址就可以了不需要监听位置的变化所以获取到了直接就remove掉了
public static void onActivityStoped(){
if(locationListener != null){
m_locationManager.removeUpdates(locationListener);
locationListener = null;
}
Log.i(logTag,"onActivityStoped");
}
5.以上获得的是经纬度,这个还需要一次转化,转化为我们熟悉的XX省XX市XX街道这种
private static String getAddress(Location location){
//用来接收位置的详细信息
List<Address> result = null;
String addressLine = "";
try {
if (location != null) {
Geocoder gc = new Geocoder(mContext, Locale.getDefault());
result = gc.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
}
} catch (Exception e) {
e.printStackTrace();
}
if(result != null && result.get(0) != null){
//这块获取到的是个数组我们取一个就好 下面是具体的方法查查API就能知道自己要什么
//result.get(0).getCountryName()
Log.i("address",addressLine);
//Toast.makeText(mContext,result.get(0).toString(),Toast.LENGTH_LONG).show();
}
return addressLine;
}
6.做到这里完成了一大半了 试着去获取下也有了,但是但是但是重要的事情说三遍安卓6.0以上的系统获取不到(悲伤地表情,查了好多地方也就介绍到这里了)我们继续
我们要做的是前面说的动态加载权限代码如下:
public static void initPermission(Context context) {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//请求权限
ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION);
}else{
startLocation(context);
}
}else{
startLocation(context);
}
}
但是加上这个就好了吗?显然并不是实际上加上这个病不起什么作用;关键在如下:
一定要实现OnRequestPermissionsResultCallback 在权限结果有了之后再初始化定位(AndroidLocation.REQUEST_CODE_LOCATION 是requestPermissions申请权限的一个自己定义的变量随便写)
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback;
public class MainActivity extends Activity implements OnRequestPermissionsResultCallback{
...
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// TODO Auto-generated method stub
if(grantResults.length == 0){
return;
}
Log.i("AndroidLocation.REQUEST_CODE_LOCATION",AndroidLocation.REQUEST_CODE_LOCATION+"==="+requestCode);
switch (requestCode) {
case AndroidLocation.REQUEST_CODE_LOCATION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Toast.makeText(MainActivity.this, "授权位置信息 Denied", Toast.LENGTH_SHORT)
// .show();
location.init(this);
}
break;
default:
//super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
...
}
原理很简单,做个备忘!
链接:https://www.jianshu.com/p/783fb8580dd4