I am trying to insert the address fetched from the geocoder
API, into my edittext
field. After granting the location permission in my app, no text gets inserted in my edittext
field, and even the SMS permission isn't called too.
My code:
public class message extends AppCompatActivity {
EditText edittext;
Button send;
FusedLocationProviderClient fusedLocationProviderClient;
ArrayList<String> contacts=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
send = findViewById(R.id.button);
edittext = findViewById(R.id.msg);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
contacts = getIntent().getStringArrayListExtra("contacts");
if(ActivityCompat.checkSelfPermission(message.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location != null){
try {
Geocoder geocoder = new Geocoder(message.this, Locale.getDefault());
List<Address> address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Log.d("address", address.get(0).getAddressLine(0));
edittext.setText(address.get(0).getAddressLine(0));
sos_message();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}else
ActivityCompat.requestPermissions(message.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
}
private void sos_message() {
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(ActivityCompat.checkSelfPermission(message.this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED){
try {
SmsManager sms = SmsManager.getDefault();
if ((contacts != null) || (edittext.getText().toString().length() != 0)) {
for (int i = 0; i < contacts.size(); i++) {
sms.sendTextMessage(String.valueOf(contacts.get(i)), null, edittext.getText().toString(), null, null);
}
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Message sent successfully", Snackbar.LENGTH_LONG);
snackbar.show();
} else {
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Please try again", Snackbar.LENGTH_LONG);
snackbar.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
else
ActivityCompat.requestPermissions(message.this, new String[]{Manifest.permission.SEND_SMS}, 44);
}
});
}
}
My AndroidManifest.xml permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
Thanks to @Kidus Tekeste, I discovered that my location
object was actually showing to be null. This piece of code solves the problem:
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10 * 1000); // 10 seconds
locationRequest.setFastestInterval(5 * 1000); // 5 seconds
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
if (location != null) {
stringBuilder.append(location.getLatitude());
stringBuilder.append("-");
stringBuilder.append(location.getLongitude());
stringBuilder.append("\n\n");
Toast.makeText(message.this,stringBuilder,Toast.LENGTH_SHORT).show();
//txtContinueLocation.setText(stringBuilder.toString());
if (fusedLocationProviderClient != null) {
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
}
}
}
};
if((ActivityCompat.checkSelfPermission(message.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
&& (ActivityCompat.checkSelfPermission(message.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) ){
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null){
Geocoder geocoder = new Geocoder(message.this, Locale.getDefault());
List<Address> address = null;
try {
address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
Log.d("address", address.get(0).getAddressLine(0));
edittext.setText(address.get(0).getAddressLine(0));
sos_message();
}
else{
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Try again", Snackbar.LENGTH_LONG);
snackbar.show();
}
}
});
}else
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION},
44);