I want to get the current location and show it on my maps activity, but I have to move the part of the code that I use to find the current location to a background service which will start when i click the button start and nothing will work from what I have tried. This is the code I used before to find the current location:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationManager manager;
private LocationListener listener;
private static final int LOCATION_PERMISSIONS_REQUEST_CODE = 3;
private MyService myService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.GoogleMap);
mapFragment.getMapAsync(this);
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
if(mMap!=null) {
LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(currentLocation));
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
getLocation();
//initialize start and cancel button
findViewById(R.id.startButton).setOnClickListener(view -> {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MyService.class);
//startService(intent);
});
findViewById(R.id.cancelButton).setOnClickListener(view -> {
Intent serviceIntent = new Intent(this, MyService.class);
//stopService(serviceIntent);
//return back to main activity
Intent mainActivity = new Intent(this, MainActivity.class);
startActivity(mainActivity);
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==LOCATION_PERMISSIONS_REQUEST_CODE) {
int count =0;
for(String permission: permissions) {
if(permission==android.Manifest.permission.ACCESS_FINE_LOCATION){
if(grantResults[count]==PackageManager.PERMISSION_GRANTED){
getLocation();
}
}
count++;
}
}
}
private void getLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 50, listener);
} else {
Toast.makeText(this, "Location permission is denied, please allow the permission", Toast.LENGTH_LONG).show();
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},LOCATION_PERMISSIONS_REQUEST_CODE);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
getLocation();
}