我寫了簡單的代碼,返回設備的gpsLocation。 當我編譯它時,我得到一個關於我沒有使用訪問GPS和網絡的'checkSelfPermission'的錯誤。爲什麼我不能在代碼中刪除'checkSelfPermission'?
當我到達'checkSelfPermission'的代碼時,我得到一個異常。
- 我該如何避免'必須'使用checkSelfPermission方法?
爲什麼我得到異常?
@TargetApi(Build.VERSION_CODES.M) private void getLocation() { 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 (isNetworkEnabled) { **// the exception is on the 'checkSelfPermission'** if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } catch(Exception e) { Log.e("Error", e.getMessage()); }
}
我不能在eclipse中使用ContextCompat.checkSelfPermission你告訴我如何使用你的解決方案在eclipse中使用運行時權限。 – Vasant
使用eclipse並沒有改變,運行期間詢問權限的方式保持不變。 –
在eclipse中,runtim permition的代碼將無法正常工作。我正在使用v4庫。您可以爲我提供最新的支持庫 – Vasant