Мне нужно занять текущую позицию устройства в начале основного действия, поэтому я использовал диспетчер местоположения и все эти вещи.Но я не могу узнать текущее положение устройства и не знаю почему.Здесь я размещаю разрешение манифеста и мой код.(полная трассировка стека такая длинная)
MANIFEST
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="com.example.mapexdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />
CODE
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(MainActivity.this, "NO GPS", Toast.LENGTH_LONG).show();
System.out.print("OknO");
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
System.out.print("OK0");
if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
System.out.print("ok01");
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 = locationManager.getLastKnownLocation(LocationManager. PASSIVE_PROVIDER);
System.out.print("ok011");
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
Toast.makeText(MainActivity.this, "Your current location is"+ lattitude + longitude, Toast.LENGTH_SHORT).show();
System.out.print("OK1");
} else if(location1 != null){
double latti = location1.getLatitude();
double longi = location1.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
System.out.print("OK2");
Toast.makeText(MainActivity.this, "Your current location is"+ lattitude + longitude, Toast.LENGTH_SHORT).show();
} else if (location2 != null){
double latti = location2.getLatitude();
double longi = location2.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
Toast.makeText(MainActivity.this, "Your current location is"+ lattitude + longitude, Toast.LENGTH_SHORT).show();
System.out.print("OK3");
} else {
Toast.makeText(MainActivity.this, "Unable to trace your location", Toast.LENGTH_SHORT).show();
System.out.print("OK4");
}
}
}