Android 6.0 동적 액세스 권한 위치
//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 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 getDeniedPermissions(Context context, String... permissions) {
List 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;
}
간단하고 난폭한 사용으로 액티브에서 권한이 필요한 곳에서 직접 사용하면 된다
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 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);
}
}
}
여기
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.