public static void requestRuntimePermission(String[] permissions, PermissionListener listener) { Activity topActivity = ActivityCollector.getTopActivity(); if (topActivity == null) { return; } mListener = listener; List<String> permissionList = new ArrayList<>(); for (String permission : permissions) { if (ContextCompat.checkSelfPermission(topActivity, permission) != PackageManager.PERMISSION_GRANTED) { permissionList.add(permission); } } if (!permissionList.isEmpty()) { ActivityCompat.requestPermissions(topActivity, permissionList.toArray(new String[permissionList.size()]), 1); } else { mListener.onGranted(); } }
简单说下上面的代码,创建一个permissionList来把一个或者多个权限add进去,通过checkSelfPermission检测权限再进行add进去再去请求权限,如果是6.0以下的就直接回调我们写好的接口mListener.onGranted();进行下一步
然后就是请求权限的回调了:
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 1: if (grantResults.length > 0) { List<String> deniedPermission = new ArrayList<>(); for (int i = 0; i < grantResults.length; i++) { int grantResult = grantResults[i]; String permission = permissions[i]; if (grantResult != PackageManager.PERMISSION_GRANTED) { deniedPermission.add(permission); } } if (deniedPermission.isEmpty()) { mListener.onGranted(); } else { mListener.onDenied(deniedPermission); } } default: break; } }
如果grantResults>0代表有请求权限,然后新建一个deniedPermissionList来装上回调失败的权限,如果deniedPermission回调权限为空就直接下一步工作mListener.onGranted();,不然就回调所失败的权限mListener.onDenied(deniedPermission);
在MainActivity中使用,代码:
` requestRuntimePermission(new String[]{Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.ACCESS_FINE_LOCATION}, new PermissionListener() { @Override public void onGranted() { Toast.makeText(MainActivity.this,"所有权限都同意了",Toast.LENGTH_SHORT).show(); }
@Override
public void onDenied(List<String> deniedPermission) {
for(String persmission:deniedPermission){
Toast.makeText(MainActivity.this,"被拒绝的权限:"+persmission,Toast.LENGTH_SHORT).show();
}
}
}); `
` private void getLocation() { try { locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); //获取所有可用的位置提供器 List providers = locationManager.getProviders(true); if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS locationProvider = LocationManager.GPS_PROVIDER; } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是Network locationProvider = LocationManager.NETWORK_PROVIDER; } else {
ToastUtils.showToast("没有可用的位置提供器");
return;
}
Location mLocation = locationManager.getLastKnownLocation(locationProvider);
if (null != mLocation) {
getGeocoder(mLocation);
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, myLocationListener);
} catch (SecurityException e) {
e.printStackTrace();
}
} `
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, myLocationListener);
-
在build.gradle里面添加google play service依赖
compile 'com.google.android.gms:play-services-location:9.0.2'
-
在AndroidManifest.xml添加
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
-
在MainActivity实现接口ConnectionCallbacks, OnConnectionFailedListener,并有回调方法 ` @Override public void onConnected(@Nullable Bundle bundle) { displayLocation(); }
@Override public void onConnectionSuspended(int i) { mGoogleApiClient.connect(); }
@Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { ToastUtils.showToast(connectionResult.getErrorMessage()); } `
-
在 onResume()写上checkPlayServices()
@Override public void onResume() { super.onResume(); checkPlayServices(); }
-
call mGoogleApiClient.connect()在onStart()方法
@Override public void onStart() { super.onStart(); if (mGoogleApiClient != null) { mGoogleApiClient.connect(); } }
-
当google api connected成功了,displayLocation()方法在onConnected()之后会获取当前的位置信息 ` private boolean checkPlayServices() { int resultCode = GooglePlayServicesUtil .isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } else { Toast.makeText(this, "This device is not supported GooglePlayServices.", Toast.LENGTH_LONG) .show(); } return false; } return true; } private void displayLocation() {
try { mLastLocation = LocationServices.FusedLocationApi .getLastLocation(mGoogleApiClient); if (mLastLocation != null) { getGeocoder(mLastLocation); } } catch (SecurityException e) { e.printStackTrace(); }
} `
-
拿到当前的经纬度之后,去call google的api获取相对应的地理位置,api: http://maps.google.cn/maps/api/geocode/json
-
更多详情请看demo