From 3a4343f647f038a9e8366f17b33a07fdfd8b680c Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期四, 05 十二月 2024 10:26:57 +0800
Subject: [PATCH] 1.添加巡检时的后台定位权限获取

---
 app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java |   83 ++++++++++++++++++++++++++++++++++-------
 1 files changed, 69 insertions(+), 14 deletions(-)

diff --git a/app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java b/app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java
index ac24cfa..c06bbe0 100644
--- a/app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java
+++ b/app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java
@@ -53,6 +53,10 @@
 import com.dayu.pipirrapp.utils.ToastUtil;
 import com.dayu.pipirrapp.utils.WebViewUtils;
 import com.dayu.pipirrapp.view.ConfirmDialog;
+import com.dayu.pipirrapp.view.TipUtil;
+import com.hjq.permissions.OnPermissionCallback;
+import com.hjq.permissions.Permission;
+import com.hjq.permissions.XXPermissions;
 import com.jeremyliao.liveeventbus.LiveEventBus;
 import com.tencent.bugly.crashreport.CrashReport;
 
@@ -300,7 +304,7 @@
 //                    }
                     } else {
                         //涓嬭浇鐡︾墖
-                        ApiManager.getInstance().donwLoadTile(MapFragment.this.getContext(),androidUrl);
+                        ApiManager.getInstance().donwLoadTile(MapFragment.this.getContext(), androidUrl);
                     }
 //                    Log.d(TAG, "鍦ㄧ嚎鍔犺浇>>>" + url);
                 }
@@ -343,7 +347,17 @@
         });
         //宸℃鎸夐挳
         binding.inspectButton.setOnClickListener(v -> {
-            chageInspecState(InspectionUtils.STAT_INSPECTION_ONCLICK);
+            if (XXPermissions.isGranted(MapFragment.this.getContext(), Permission.ACCESS_BACKGROUND_LOCATION)) {
+                chageInspecState(InspectionUtils.STAT_INSPECTION_ONCLICK);
+            } else {
+                TipUtil.show(MapFragment.this.getActivity(), "宸℃瀹氫綅闇�瑕佹偍閫夋嫨\"濮嬬粓鍏佽\"瀹氫綅淇℃伅锛屽惁鍒欐棤娉曞贰妫�銆�", new TipUtil.TipListener() {
+                    @Override
+                    public void onCancle() {
+                        getPermissions();
+                    }
+                });
+            }
+
         });
         //鏆傚仠宸℃
         binding.inspectPause.setOnClickListener(v -> {
@@ -522,18 +536,21 @@
                         confirmDialog.dismiss();
                         chageInspecState(InspectionUtils.NO_INSPECTION);
                         SharedPreferencesHelper.getInstance(MapFragment.this.getContext()).put(CommonKeyName.inspectionState, InspectionUtils.NO_INSPECTION);
-                        //鏇存柊鏈湴鏁版嵁搴撶粨鏉熸椂闂�
-                        mInspectionBean.setStopTime(DateUtils.getNowDateStr());
-                        InspectionUtils.upataInspectionData(MapFragment.this.getContext(), mInspectionBean);
-                        //涓婃姤缁撴潫鏃堕棿
-                        InspectionLocationBean inspectionLocationBean = InspectionUtils.createInspectionLocation(lastLatLonBean, mInspectionBean);
-                        InspectionUtils.addInspectionLocationData(MapFragment.this.getContext(), inspectionLocationBean);
-                        pushLocationData(inspectionLocationBean);
-                        mInspectionBean = new InspectionBean();
-                        //娓呴櫎鍦板浘宸℃杞ㄨ抗
-                        mWebView.evaluateJavascript("javascript:cleanLocationLay()", value -> {
-                        });
-                        lastLatLonBean = null;
+                        //鏈夊彲鑳借繕娌¤幏鍙栧埌瀹氫綅淇℃伅灏辩粨鏉熷贰妫�浜�
+                        if (lastLatLonBean != null) {
+                            //鏇存柊鏈湴鏁版嵁搴撶粨鏉熸椂闂�
+                            mInspectionBean.setStopTime(DateUtils.getNowDateStr());
+                            InspectionUtils.upataInspectionData(MapFragment.this.getContext(), mInspectionBean);
+                            //涓婃姤缁撴潫鏃堕棿
+                            InspectionLocationBean inspectionLocationBean = InspectionUtils.createInspectionLocation(lastLatLonBean, mInspectionBean);
+                            InspectionUtils.addInspectionLocationData(MapFragment.this.getContext(), inspectionLocationBean);
+                            pushLocationData(inspectionLocationBean);
+                            mInspectionBean = new InspectionBean();
+                            //娓呴櫎鍦板浘宸℃杞ㄨ抗
+                            mWebView.evaluateJavascript("javascript:cleanLocationLay()", value -> {
+                            });
+                            lastLatLonBean = null;
+                        }
                     }
                 });
                 confirmDialog.show();
@@ -590,4 +607,42 @@
         SharedPreferencesHelper.getInstance(this.getContext()).put(CommonKeyName.inspectionState, InspectionUtils.STAT_INSPECTION);
     }
 
+
+    /**
+     * 鐢宠鍚庡彴瀹氫綅鏉冮檺
+     */
+    private void getPermissions() {
+        try {
+            XXPermissions.with(MapFragment.this.getContext())
+                    // 鐢宠澶氫釜鏉冮檺
+                    .permission(Permission.ACCESS_BACKGROUND_LOCATION)
+                    .request(new OnPermissionCallback() {
+
+                        @Override
+                        public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
+                            if (allGranted) {
+                                chageInspecState(InspectionUtils.STAT_INSPECTION_ONCLICK);
+                            }
+                        }
+
+                        @Override
+                        public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
+                            if (doNotAskAgain) {
+                                // 濡傛灉鏄姘镐箙鎷掔粷灏辫烦杞埌搴旂敤鏉冮檺绯荤粺璁剧疆椤甸潰
+                                TipUtil.show(MapFragment.this.getActivity(), "琚案涔呮嫆缁濇巿鏉冿紝璇锋墜鍔ㄦ巿浜堝畾浣嶅缁堝厑璁告潈闄�!閫夋嫨鈥滄潈闄愨��-->鈥滀綅缃俊鎭��-->鈥滃缁堝厑璁糕��,鐒跺悗杩斿洖搴旂敤銆�", new TipUtil.TipListener() {
+                                    @Override
+                                    public void onCancle() {
+                                        XXPermissions.startPermissionActivity(MapFragment.this.getContext(), permissions);
+                                    }
+                                });
+                            } else {
+                                ToastUtil.showToastLong(MapFragment.this.getContext(), "鑾峰彇瀹氫綅濮嬬粓鍏佽鏉冮檺澶辫触");
+                            }
+                        }
+                    });
+
+        } catch (Throwable e) {
+            e.printStackTrace();
+        }
+    }
 }

--
Gitblit v1.8.0