Browse Source

1.android10以及以上申请后台定位权限。

20241218TB223FC(测试jar包)
wangwanlei 8 months ago
parent
commit
e73248cfbc
1 changed files with 10 additions and 4 deletions
  1. 10
    4
      corelib/src/main/java/com/xhly/corelib/utils/SystemUtil.java

+ 10
- 4
corelib/src/main/java/com/xhly/corelib/utils/SystemUtil.java View File

@@ -266,10 +266,16 @@ public class SystemUtil {
266 266
             LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
267 267
                   String bestProvider = "";
268 268
             List<String> providers = locationManager.getProviders(true);
269
-            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
270
-                    ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
271
-               &&ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_BACKGROUND_LOCATION)== PackageManager.PERMISSION_GRANTED
272
-            ) {
269
+            boolean flag=false;
270
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
271
+                flag=( ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
272
+                        ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
273
+                        &&ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_BACKGROUND_LOCATION)== PackageManager.PERMISSION_GRANTED);
274
+            }else {
275
+                flag=( ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
276
+                        ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED);
277
+            }
278
+            if (flag) {
273 279
                 for (String provider : providers) {
274 280
                     Location lastKnownLocation =locationManager.getLastKnownLocation(provider);
275 281
                     if (lastKnownLocation==null){

Loading…
Cancel
Save