在下面的代码中,我无法解析 if block 内第 11 行的 MY_PERMISSION_ACCESS_COURSE_LOCATION,我检查了所有可能的内容,并在依赖项中编译了 google play 服务,但无法解析:| 我应该怎么办 ? 我实际上每 5 分钟跟踪一次用户的位置并显示它。 错误是
Error:(60, 41) error: cannot find symbol variable MY_PERMISSION_ACCESS_COURSE_LOCATION
谁能帮帮我吗?
public Location getLocation() {
try {
if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION )
!= PackageManager.PERMISSION_GRANTED )
{
ActivityCompat.requestPermissions( this, new String[]
{ android.Manifest.permission.ACCESS_COARSE_LOCATION
},
LocationServices.MY_PERMISSION_ACCESS_COURSE_LOCATION
);
Toast.makeText(GPSTracker.this, "open gps", Toast.LENGTH_SHORT).show();
}
Log.i("ok", "outside IF location");
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
10000, // 3 sec
10, this);
// 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
Toast.makeText(GPSTracker.this, "open it", Toast.LENGTH_SHORT).show();
}
this.canGetLocation = true;
if (isNetworkEnabled) {
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 GPS Enabled get lat/long using GPS Services
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) {
e.printStackTrace();
}
return location;
}
最佳答案
我只是检查您是否指的是 MY_PERMISSION_ACCESS_COARSE_LOCATION 而不是 MY_PERMISSION_ACCESS_COURSE_LOCATION
关于java - 无法解析 MY_PERMISSION_ACCESS_COURSE_LOCATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38880788/