2017-02-09 75 views
1

我不能够证明上述4.4.2在哪里增加运行时间许可version.Still困惑在谷歌地图的确切位置?请帮助无法在API级别23以上添加运行时权限?

GpsLocation类

public class GpsLocation extends Service implements LocationListener { 
    private final Context mContext; 
    // flag for GPS status 
    boolean isGPSEnabled = false; 
    // flag for network status 
    boolean isNetworkEnabled = false; 
    // flag for GPS status 
    boolean canGetLocation = false; 
    Location location; // location 
    double latitude; // latitude 
    double longitude; // longitude 
    double speed, direction; 
    // The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 
    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 
    // Declaring a Location Manager 
    protected LocationManager locationManager; 

    public GpsLocation(Context context) { 
     this.mContext = context; 
     getLocation(); 
    } 

    public Location getLocation() { 

     if (Build.VERSION.SDK_INT >= 23 && 
       ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
       ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     } 




     try { 
      locationManager = (LocationManager) mContext 
        .getSystemService(LOCATION_SERVICE); 
      // getting GPS status 
      isGPSEnabled = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 
      // getting network status 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 
      } else { 
       this.canGetLocation = true; 
       // First get location from Network Provider 
       if (isNetworkEnabled) { 

        if (isGPSEnabled) { 




         locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 


       } 
       // if GPS Enabled get lat/long using GPS Services 



        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 



     return location; 
    } 

    /** 
    * Stop using GPS listener 
    * Calling this function will stop using GPS in your app 
    */ 
    public void stopUsingGPS() { 
     if (locationManager != null) { 
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // 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; 
      } 
      locationManager.removeUpdates(GpsLocation.this); 
     } 
    } 

    /** 
    * Function to get latitude 
    */ 
    public double getLatitude() { 
     if (location != null) { 
      latitude = location.getLatitude(); 
     } 
     // return latitude 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    */ 
    public double getLongitude() { 
     if (location != null) { 
      longitude = location.getLongitude(); 
     } 
     // return longitude 
     return longitude; 
    } 

    public double getSpeed() { 
     return speed; 
    } 

    public double getDirection() { 
     return direction; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    * 
    * @return boolean 
    */ 
    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 

    /** 
    * Function to show settings alert dialog 
    * On pressing Settings button will launch Settings Options 
    */ 
    public void showSettingsAlert() { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 
     // Setting Dialog Title 
     alertDialog.setTitle("GPS is settings"); 
     // Setting Dialog Message 
     alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 
     // On pressing Settings button 
     alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       mContext.startActivity(intent); 
      } 
     }); 
     // on pressing cancel button 
     alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
      } 
     }); 
     // Showing Alert Message 
     alertDialog.show(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      speed = location.getSpeed(); 
      direction = location.getBearing(); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 
} 

NearMe类

public class NearMe extends Fragment { 
    MapView mMapView; 
    private GoogleMap googleMap; 
    GpsLocation gpsLocation; 
    double longitude, latitude; 
    private ProgressDialog pDialog; 
    ArrayList<MySchool> al_school = new ArrayList<MySchool>(); 
    ArrayList<MyCollege> al_college = new ArrayList<MyCollege>(); 
    ArrayList<MyUniversity> al_university = new ArrayList<MyUniversity>(); 

    private static final String TAG = NearMe.class.getSimpleName(); 

    private static final String urlSchool = "http://www.**.com/api/v1/schools"; 
    private static final String urlCollege = "http://www.**.com/api/v1/colleges"; 
    private static final String urlUniversity = "http://www.**.com/api/v1/universities"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     final View v = inflater.inflate(R.layout.fragment_near_me, container, false); 

     final LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 


     if (isConnected()) { 
      if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
       Toast.makeText(getContext(), "GPS is Enabled in your device", Toast.LENGTH_SHORT).show(); 

       mMapView = (MapView) v.findViewById(R.id.mapView); 
       mMapView.onCreate(savedInstanceState); 

       mMapView.onResume(); 

       try { 
        MapsInitializer.initialize(getActivity().getApplicationContext()); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       gpsLocation = new GpsLocation(getContext()); 


       if (gpsLocation.canGetLocation()) { 
        longitude = gpsLocation.getLongitude(); 
        latitude = gpsLocation.getLatitude(); 

        Toast.makeText(getContext(), "latitude:" + latitude + "Longitude:" + longitude, Toast.LENGTH_LONG).show(); 
       } 


       pDialog = new ProgressDialog(getContext()); 
       pDialog.setMessage("Loading…"); 
       pDialog.show(); 


       mMapView.getMapAsync(new OnMapReadyCallback() { 
        @Override 
        public void onMapReady(GoogleMap mMap) { 
         googleMap = mMap; 

         CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(new LatLng(gpsLocation.getLatitude(), gpsLocation.getLongitude())); 
         CameraUpdate zoom = CameraUpdateFactory.zoomTo(14); 
         mMap.moveCamera(cameraUpdate); 
         mMap.animateCamera(zoom); 

         // mMap.animateCamera(cameraUpdate); 


         // LatLng schoollatlng = new LatLng(latitude, longitude); 
         // googleMap.addMarker(new MarkerOptions().position(schoollatlng).title("MyLocation")); 
         // CameraPosition cameraPosition = new CameraPosition.Builder().target(schoollatlng).zoom(11).build(); 
         // googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
         // googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gpsLocation.getLatitude(), gpsLocation.getLongitude()), 14.0f)); 

         drawSchoolMarker(); 
         drawCollegeMarker(); 
         drawUniversityMarker(); 
        } 
       }); 
      } else { 
       showGPSDisabledAlertToUser(); 
      } 
     } else { 

      Toast.makeText(getContext(), "Please check your internet connection", Toast.LENGTH_LONG).show(); 
     } 

     return v; 
    } 

    public boolean isConnected() { 
     ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Activity.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
     if (networkInfo != null && networkInfo.isConnected()) 
      return true; 
     else 
      return false; 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 

    private void drawSchoolMarker() { 

     JsonArrayRequest schoolRequest = new JsonArrayRequest(urlSchool, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 

         hidePDialog(); 

         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 

           JSONObject obj = response.getJSONObject(i); 
           MySchool school = new MySchool(); 

           school.setId(obj.getInt("id") + " "); 
           school.setName(obj.getString("name")); 
           if (obj.getString("latitude") == "null") { 
            school.setLatitude(Double.parseDouble("27.6712")); 
           } else { 
            school.setLatitude(Double.parseDouble(obj.getString("latitude"))); 
           } 

           if (obj.getString("longitude") == "null") { 
            school.setLongitude(Double.parseDouble("85.2865")); 
           } else { 
            school.setLongitude(Double.parseDouble(obj.getString("longitude"))); 
           } 

           al_school.add(school); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 

         //iterate from arraylist 
         for (MySchool schoolItr : al_school) { 

          View marker = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker, null); 
          TextView numTxt = (TextView) marker.findViewById(R.id.num_txt); 
          numTxt.setText(schoolItr.getName()); 
          LatLng latlng = new LatLng(schoolItr.getLatitude(), schoolItr.getLongitude()); 
          googleMap.addMarker(new MarkerOptions().position(latlng).icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(getContext(), marker))).title(schoolItr.getName())); 
         } 

        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 
      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(schoolRequest); 
    } 

    private void drawCollegeMarker() { 
     JsonArrayRequest collegeRequest = new JsonArrayRequest(urlCollege, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 

         hidePDialog(); 

         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 

           JSONObject obj = response.getJSONObject(i); 
           MyCollege college = new MyCollege(); 

           college.setId("" + obj.getInt("id")); 
           college.setName("" + obj.getString("name")); 
           //college.setLatitude(Double.parseDouble("" + obj.getDouble("latitude"))); 
           //college.setLongitude(Double.parseDouble("" + obj.getDouble("longitude"))); 
           if (obj.getString("latitude") == "null") { 

            college.setLatitude(Double.parseDouble("27.6712")); 
           } else { 

            college.setLatitude(Double.parseDouble(obj.getString("latitude"))); 
           } 

           if (obj.getString("longitude") == "null") { 
            college.setLongitude(Double.parseDouble("27.6712")); 
           } else { 
            college.setLongitude(Double.parseDouble(obj.getString("longitude"))); 

           } 

           al_college.add(college); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 

         //iterate from arraylist 
         for (MyCollege collegeItr : al_college) { 

          View marker = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_college, null); 
          TextView numTxt = (TextView) marker.findViewById(R.id.txt_college); 
          numTxt.setText(collegeItr.getName()); 
          LatLng latlng = new LatLng(collegeItr.getLatitude(), collegeItr.getLongitude()); 
          googleMap.addMarker(new MarkerOptions().position(latlng).icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(getContext(), marker))).title(collegeItr.getName())); 
         } 


        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 
      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(collegeRequest); 
    } 

    private void drawUniversityMarker() { 
     JsonArrayRequest uniRequest = new JsonArrayRequest(urlUniversity, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 

         hidePDialog(); 

         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 

           JSONObject obj = response.getJSONObject(i); 
           MyUniversity university = new MyUniversity(); 

           university.setId("" + obj.getInt("id")); 
           university.setName("" + obj.getString("name")); 
           //university.setLatitude(Double.parseDouble("" + obj.getDouble("latitude"))); 
           //university.setLongitude(Double.parseDouble("" + obj.getDouble("longitude"))); 
           if (obj.getString("latitude") == "null") { 

            university.setLatitude(Double.parseDouble("27.6712")); 
           } else { 

            university.setLatitude(Double.parseDouble(obj.getString("latitude"))); 
           } 

           if (obj.getString("longitude") == "null") { 
            university.setLongitude(Double.parseDouble("27.6712")); 
           } else { 
            university.setLongitude(Double.parseDouble(obj.getString("longitude"))); 

           } 

           al_university.add(university); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 

         //iterate from arraylist 
         for (MyUniversity universityItr : al_university) { 

          View marker = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_university, null); 
          TextView numTxt = (TextView) marker.findViewById(R.id.txt_university); 
          numTxt.setText(universityItr.getName()); 
          LatLng latlng = new LatLng(universityItr.getLatitude(), universityItr.getLongitude()); 
          googleMap.addMarker(new MarkerOptions().position(latlng).icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(getContext(), marker))).title(universityItr.getName())); 

         } 


        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 
      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(uniRequest); 
    } 

    public static Bitmap createDrawableFromView(Context context, View view) { 
     DisplayMetrics displayMetrics = new DisplayMetrics(); 
     ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
     view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
     view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); 
     view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); 
     view.buildDrawingCache(); 
     Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 

     Canvas canvas = new Canvas(bitmap); 
     view.draw(canvas); 

     return bitmap; 
    } 

    private void showGPSDisabledAlertToUser() { 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); 
     alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?") 
       .setCancelable(false) 
       .setPositiveButton("Goto Settings Page To Enable GPS", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           Intent callGPSSettingIntent = new Intent(
             android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
           startActivity(callGPSSettingIntent); 
          } 
         }); 
     alertDialogBuilder.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
        } 
       }); 
     AlertDialog alert = alertDialogBuilder.create(); 
     alert.show(); 
    } 
} 

确切的问题在哪里?以及为什么地图不显示以及SDK 23以上?版本

+0

你会得到什么错误? – d1vivek681065

+0

尝试此链接它可以帮助你http://stackoverflow.com/questions/34342816/android-6-0-multiple-permissions –

+0

到目前为止,我只能告诉你只是检查权限,如果它没有被授予。你在哪里要求他们?在访问服务的getLocation方法之前,您应该在活动中执行此操作。 – d1vivek681065

回答

1

尝试添加这段代码:

if (Build.VERSION.SDK_INT >= 23 && 
     ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
     ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

    // this says , Permissions are not granted , so ask for permissions here if not granted and look out for permission callback if user grants 
} 
+0

我必须添加这个? if(ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){ //我们应该显示一个解释吗? 如果(ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)){ }否则{ ActivityCompat.requestPermissions(thisActivity, 新的String [] {Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS ); } } – seon

1

你可能是从某个地方明确甚至有关于如何做到这一点的注释复制的代码。

你只有检查权限,但你永远不会请求他们。因此你永远不会有位置权限。

请参阅Android文档,了解更多有关ActivityCompat.requestPermissions()

+0

我在哪里实现请求权限? – seon

+0

@seon请阅读您复制的代码。对此有一个明确的评论。虽然那是错误的地方。如果你不知道它甚至会干什么,请不要复制随机代码。你会遇到麻烦。只要阅读文档,它就会解释做什么和在哪里。 –

+0

我知道我必须添加权限吗?但不知道我要放置在哪里?所以它令我困惑 – seon

1

您可以通过下面的代码

if(Build.VERSION.SDK_INT >= 23) 
      if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       if (ActivityCompat.shouldShowRequestPermissionRationale(MenuActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) { 
        // permission wasn't granted 
       } else { 
        ActivityCompat.requestPermissions(MenuActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_PERMISSION); 
       } 
       if (ActivityCompat.shouldShowRequestPermissionRationale(MenuActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)) { 
        // permission wasn't granted 
       } else { 
        ActivityCompat.requestPermissions(MenuActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE_PERMISSIONN); 
       } 
      } 
@Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     if (requestCode == REQUEST_CODE_PERMISSION) { 
      if (grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       // permission was granted 
      } else { 
       // permission wasn't granted 
      } 
     } 
     if (requestCode == REQUEST_CODE_PERMISSIONN) { 
      if (grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       // permission was granted 
      } else { 
       // permission wasn't granted 
      } 
     } 
    } 

添加以下属性请求权限您的清单

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-feature android:name="android.hardware.location.gps" /> 
+0

我已经提到这个http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/教程。所以我必须添加这个?请详细告知 – seon

+0

以显示地图,您是否可以通过https://console.developers.google创建项目。com/apis/library和[创建凭证] – Harmantj

+0

是的。显示在较低的版本,但不是在更高版本 – seon

1

以上API级别23,你将有权务实,如:

将SEND_SMS权限更改为您的谷歌地图的许可

private static final int PERMISSION_REQUEST_CODE = 1; 

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { 

if (checkSelfPermission(Manifest.permission.SEND_SMS) 
     == PackageManager.PERMISSION_DENIED) { 

    Log.d("permission", "permission denied to SEND_SMS - requesting it"); 
    String[] permissions = {Manifest.permission.SEND_SMS}; 

    requestPermissions(permissions, PERMISSION_REQUEST_CODE); 

} 

}

+0

如何检查给定的权限的响应,如权限被授予或没有:) – user7176550

+0

因为你必须使用OnRequestPermissionResult覆盖方法 –

+0

好的谢谢:) – user7176550

0

在这个方法中的getLocation。


public Location getLocation() { 

    if (ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
      ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE); 
    } 
//Do your thing 
} 

,并听取权限授予或拒绝重载函数onRequestPermissionsResult()在您的活动/片段这样


@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
} 

另外的方法你正在使用似乎有点老。请结帐此链接 https://developer.android.com/training/location/receive-location-updates.html

0

每当getLocation()梅索德电话,它总是检查权限,按您的代码在这里

if (Build.VERSION.SDK_INT >= 23 && 
      ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
      ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

    } 

如果权限不是在manifest文件名为意味着你没有提到所需的权限,这可能是首先检查的地方,如果没有给它不会更新呼叫请求许可

许可 我刚才检查,如果你不提许可文件,它不会要求允许

双方将合并,并要求单独许可

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 添加这两权限Manifest文件

将此代码添加到getLocation()方法开始

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
       ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions((MainActivity.this), new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQ_PERMISSION_CODE); 
      return; 
     } 
    } 

就是这样,直到许可给予该方法不会执行

*请原谅我的英语

+0

可以请你提供一些好的链接 – seon

+0

我可以给你代码链接,我不知道 –

相关问题