Android6.0 以上手机在获取一些权限时候,不像23以下的机型,只需要在AndroidManifast文件下进行配置就好了,如果你设置了targetSdkVersion >23 ,则需要在运行时动态获取相应权限,下面方法类可以直接使用
//XPermissionUtils
public static int PHONE = 0x00;//电话
public static int LOCATION = 0x01;//位置
public static int CAMERA = 0x02;//相机
public static int AUDIO = 0x03;//语音
public static int EXTERNAL = 0x04;//存储
private static int mRequestCode = -1;
private static OnPermissionListener mOnPermissionListener;
public interface OnPermissionListener {
void onPermissionGranted();
void onPermissionDenied();
}
@TargetApi(Build.VERSION_CODES.M)
public static void requestPermissions(Context context, int requestCode
, String[] permissions, OnPermissionListener listener) {
if (context instanceof Activity) {
mOnPermissionListener = listener;
List<String> deniedPermissions = getDeniedPermissions(context, permissions);
if (deniedPermissions.size() > 0) {
mRequestCode = requestCode;
((Activity) context).requestPermissions(deniedPermissions
.toArray(new String[deniedPermissions.size()]), requestCode);
} else {
if (mOnPermissionListener != null)
mOnPermissionListener.onPermissionGranted();
}
} else {
throw new RuntimeException("Context must be an Activity");
}
}
/**
* 获取请求权限中需要授权的权限
*/
private static List<String> getDeniedPermissions(Context context, String... permissions) {
List<String> deniedPermissions = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) {
deniedPermissions.add(permission);
}
}
return deniedPermissions;
}
/**
* 请求权限结果,对应Activity中onRequestPermissionsResult()方法。
*/
public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (mRequestCode != -1 && requestCode == mRequestCode) {
if (mOnPermissionListener != null) {
if (verifyPermissions(grantResults)) {
mOnPermissionListener.onPermissionGranted();
} else {
mOnPermissionListener.onPermissionDenied();
}
}
}
}
/**
* 验证所有权限是否都已经授权
*/
private static boolean verifyPermissions(int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
使用简单粗暴,直接在activity中需要获取权限的地方使用即可
XPermissionUtils.requestPermissions(this, 1, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, new XPermissionUtils.OnPermissionListener() {
@Override
public void onPermissionGranted() {
//权限获取成功,进行你需要的操作
}
@Override
public void onPermissionDenied() {
//权限获取失败,进行你需要的操作
}
});
//注意,需要在activity中调用以下方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
XPermissionUtils.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
以下是位置获取的方法类
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class LocationUtils{
private staticStringTAG="LocationUtil";
public static voidrequestLocation(finalContext context) {
XPermissionUtils.requestPermissions(context,RequestCode.LOCATION, newString[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,}
, newXPermissionUtils.OnPermissionListener() {
@Override
public voidonPermissionGranted() {
startLocation(context);
}
@Override
public voidonPermissionDenied() {
DialogUtil.showAlertDialog(context,"位置");
}
});
}
private static voidstartLocation(Context context) {
//获取地理位置管理器
LocationManager locationManager = (LocationManager) context.getSystemService(Context
.LOCATION_SERVICE);
//获取所有可用的位置提供器
List providers = locationManager.getProviders(true);
if(providers ==null)return;
//获取Location
if(ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(context,Manifest
.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
List <String>providerSortList =new ArrayList<>();//对提供者进行排序,gps、net、passive
if(providers.contains(LocationManager.GPS_PROVIDER)) {
Log.d(TAG,"GPS_PROVIDER");
providerSortList.add(LocationManager.GPS_PROVIDER);
}
if(providers.contains(LocationManager.NETWORK_PROVIDER)) {
Log.d(TAG,"NETWORK_PROVIDER");
providerSortList.add(LocationManager.NETWORK_PROVIDER);
}
if(providers.contains(LocationManager.PASSIVE_PROVIDER)) {
Log.d(TAG,"PASSIVE_PROVIDER");
providerSortList.add(LocationManager.PASSIVE_PROVIDER);
}
String locationProvider ="";
for(inti =0;i < providerSortList.size();i++) {
String provider = providerSortList.get(i);
Log.d(TAG,"正在尝试:"+ provider);
Location location = locationManager.getLastKnownLocation(provider);
if(location !=null) {
locationProvider = provider;
Log.d(TAG,"定位成功:"+ provider);
saveLocation(location);
break;
}else{
Log.d(TAG,"定位失败:"+ provider);
}
}
if(!TextUtils.isEmpty(locationProvider)) {
locationManager.requestLocationUpdates(locationProvider,3000,1,locationListener);
}
}
staticLocationListenerlocationListener=newLocationListener() {
@Override
public voidonStatusChanged(String provider, intstatus,Bundle arg2) {
}
@Override
public voidonProviderEnabled(String provider) {
}
@Override
public voidonProviderDisabled(String provider) {
}
@Override
public voidonLocationChanged(Location location) {
//如果位置发生变化,重新显示
saveLocation(location);
}
};
/**
* 保存地理位置经度和纬度信息
*/
private static voidsaveLocation(Location location) {
if(location !=null) {
doublelatitude = location.getLatitude();
doublelongitude = location.getLongitude();
Log.d(TAG,"latitude:"+ latitude);
Log.d(TAG,"longitude:"+ longitude);
}
}
}
参考这里