diff --git a/BaiduLocationSample/app/src/main/AndroidManifest.xml b/BaiduLocationSample/app/src/main/AndroidManifest.xml index c231466..a612a56 100644 --- a/BaiduLocationSample/app/src/main/AndroidManifest.xml +++ b/BaiduLocationSample/app/src/main/AndroidManifest.xml @@ -2,6 +2,25 @@ + + + + + + + + + + + + + + + + + + + = Build.VERSION_CODES.M) { + ArrayList permissions = new ArrayList(); + /*** + * 定位权限为必须权限,用户如果禁止,则每次进入都会申请 + */ + // 定位精确位置 + if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ + permissions.add(Manifest.permission.ACCESS_FINE_LOCATION); + } + if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){ + permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION); + } + /* + * 读写权限和电话状态权限非必要权限(建议授予)只会申请一次,用户同意或者禁止,只会弹一次 + */ + // 读写权限 + if (addPermission(permissions, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + permissionInfo += "Manifest.permission.WRITE_EXTERNAL_STORAGE Deny \n"; + } + // 读取电话状态权限 + if (addPermission(permissions, Manifest.permission.READ_PHONE_STATE)) { + permissionInfo += "Manifest.permission.READ_PHONE_STATE Deny \n"; + } + + if (permissions.size() > 0) { + requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST); + } + } + } + + @TargetApi(23) + private boolean addPermission(ArrayList permissionsList, String permission) { + if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 如果应用没有获得对应权限,则添加到列表中,准备批量申请 + if (shouldShowRequestPermissionRationale(permission)){ + return true; + }else{ + permissionsList.add(permission); + return false; + } + + }else{ + return true; + } + } + + @TargetApi(23) + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + // EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } @Override @@ -44,7 +115,6 @@ public String generateLocationInfoV2(BDLocation location){ * 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来,每次回调的时间; * location.getTime() 是指服务端出本次结果的时间,如果位置不发生变化,则时间不变 */ - sb.append(location.getTime()); sb.append("\nlocationID : ");// LocationID sb.append(location.getLocationID()); diff --git a/BaiduLocationSample/app/src/main/res/layout/activity_main.xml b/BaiduLocationSample/app/src/main/res/layout/activity_main.xml index 9b7af1f..10ae31a 100644 --- a/BaiduLocationSample/app/src/main/res/layout/activity_main.xml +++ b/BaiduLocationSample/app/src/main/res/layout/activity_main.xml @@ -4,6 +4,12 @@ android:orientation="vertical" android:layout_height="match_parent"> +