My phone xiaomi redmi note 2, have a lollipop operating system (API 21). How to take the coordinates of my points and translate them to the corresponding address? Why in my gadget can not run and error "reverse geo fail" appears, but in other gadgets run very well. I have minSdkVersion 21 in settings gradle.
the following code
public class Emergency extends AppCompatActivity implements LocationListener {
protected LocationManager locationManager;
ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emergency);
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
}
}
//onClick button
public void btn_pick_up(View view) {
android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
loading = ProgressDialog.show(Emergency.this, "Loading", "Please wait", false, false);
getLocation();
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {}
});
android.support.v7.app.AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
void getLocation() {
try {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
assert locationManager != null;
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, this);
} catch(SecurityException e) {
e.printStackTrace();
loading.dismiss();
}
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(Emergency.this, "Latitude: " + location.getLatitude() + "\n Longitude: " + location.getLongitude(), Toast.LENGTH_LONG).show();
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Toast.makeText(Emergency.this,
addresses.get(0).getAddressLine(0) +", "+
addresses.get(0).getAddressLine(1) +", "+
addresses.get(0).getAddressLine(2),
Toast.LENGTH_LONG).show();
locationManager.removeUpdates(this);
loading.dismiss();
} catch(Exception e) {
Toast.makeText(Emergency.this, e.getMessage(), Toast.LENGTH_LONG).show();
loading.dismiss();
}
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(Emergency.this, "Please Enable GPS and Internet", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(Emergency.this, "GPS Enable", Toast.LENGTH_SHORT).show();
}
}