Abdul Smith Mustafi
Lurker
I am developing an app where gps coordinates (longitude and latitude) are sent back automatically to the person that sent a message in the first hand. I am testing on both 2 emulators and a smartphone. On an emulator, to hit the location service and send back the coordinates, i must press send in the location settings of the emulator. If i don't, the coordinates wont get fetched. How should i make it so that when Emulator A sends a message to Emulator B, Emulator B automatically sends the coordinates back to A as text Message without sending the coordinates on the emulator? Thanks
In the OnCreate Method:
Outside the OnCreate:
P.s I also tried calling the listener in the on receive but still didn't work :/
In the OnCreate Method:
Code:
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.SEND_SMS}, 0);
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.RECEIVE_SMS}, 0);
// getGPS();
// something();
// Toast.makeText(MainActivity.this, "OnCreate Working", Toast.LENGTH_SHORT).show();
if(ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
//Ask user for permission if android OS us newer than lollipop
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_GPS_PERMISSION);
}
else
{
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
else
{
getGPS();
}
}
Outside the OnCreate:
Code:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//callback user permission
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == REQUEST_GPS_PERMISSION)
{
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
{
//if permission granted by the user
getGPS();
something();
}
else
{
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
public void getGPS() {
Toast.makeText(MainActivity.this, "Getting GPS Test", Toast.LENGTH_SHORT).show();
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(MainActivity.this, "Got GPS Location", Toast.LENGTH_SHORT).show();
location.getLatitude();
location.getLongitude();
strLongitude = location.convert(location.getLongitude(), location.FORMAT_DEGREES);
strLatitude = location.convert(location.getLatitude(), location.FORMAT_DEGREES);
myLocation = "Latitude: " + strLatitude + " Longitude: " + strLongitude;
Toast.makeText(MainActivity.this, myLocation, Toast.LENGTH_SHORT).show();
// SmsManager smsManager = SmsManager.getDefault();
// Send a text based SMS
// smsManager.sendTextMessage(senderNum, null, myLocation, null, null);
}
public void something() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
Toast.makeText(MainActivity.this, "Started GPS", Toast.LENGTH_SHORT).show();
}
P.s I also tried calling the listener in the on receive but still didn't work :/