Issue
I'm trying to get the location from the GPS and display it on the screen, so far I have this code :
public class MainActivity extends AppCompatActivity implements LocationListener {
EditText el;
TextView output;
LocationManager lm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
el = (EditText) findViewById(R.id.editText);
output = (TextView) findViewById(R.id.textView2);
output.setMovementMethod(new ScrollingMovementMethod());
output.setText("");
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
output.append("Start");
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_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
output.append("Initially Failed to Grand Permission\n");
return;
}
output.append("\nOutside Permission controle");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this);
output.append("\nYoo");
}
public void send(View v) {
MessageSender messageSender = new MessageSender();
messageSender.execute(el.getText().toString());
output.append("Hello\n");
}
@Override
public void onLocationChanged(@NonNull Location location) {
output.append("Inside location function");
output.append(String.valueOf(location.getLongitude()));
}
}
I have also added permissions for fine location and coarse location in the android manifest. What am I missing?
I'm using Samsung galaxy S10
Note When I use the emulator on android studio, it shows the location, but when I use the usb debugger, and run it on my android device, it doesn't show the location
Also Even when I run it on the emulator, the location doesn't update when I move my laptop, why?
Solution
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this);
causes an error if you have not allowed ACCESS_FINE_LOCATION
and ACCESS_COARSE_LOCATION
so you have to ask those permissions. You can do that like this:
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_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this);
}
Notice that &&
has changed to ||
. Basically this code asks permissions if they have not been granted. If permissions have been granted, call the locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this);
What if permissions have not been granted? You need to wait until permissions have been granted before you can call requestLocationUpdates
. This method get called after permissions have been granted.
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this);
return;
}
}
}
Add this method into the activity. I tested this code with a physical device and it seems to work as it should.
Answered By - Matias Lappalainen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.