I need to take the current location of my mobile device, I have searched the web but all are managed in activity but I am doing it in a fragment so I get errors, the truth is that I am new to this. Strictly the error is given to me in the LocationManager mlocManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); and the error it gives me is the following
error: method getSystemService in class ContextCompat can not be applied to given types; required: Context, Class found: String reason: can not infer type-variable (s) T (current and formal argument lists differ in length) where T is a type-variable: T extends Object declared in method getSystemService (Context, Class)
I'll give you my code just in case you can give me a hand.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
double latitud = 0.0;
double longitud = 0.0;
//CONSULTO SI LA APLICACIÓN TIENE ACCESO A LA UBICACIÓN DEL DISPOSITIVO MÓVIL, EN CASO DE NO TENERLO PIDO PERMISO PARA OBTENERLA
if ((ActivityCompat.checkSelfPermission(requireContext(), permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission(requireContext(), permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions((Activity) requireContext(), new String[]{permission.ACCESS_FINE_LOCATION,}, 1000);
} else {
locationStart();
}
}
private void locationStart() {
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Localizacion Local = new Localizacion();
Local.setMainActivity(this);
assert mlocManager != null;
final boolean gpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
}
if ((PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(requireContext(), permission.ACCESS_FINE_LOCATION)) && (ActivityCompat.checkSelfPermission(requireContext(), permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions((Activity) requireContext(), new String[]{permission.ACCESS_FINE_LOCATION,}, 1000);
return;
}
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (LocationListener) Local);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) Local);
//latitud.setText("Localización agregada");
// mensaje.setText("");
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 1000) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
locationStart();
return;
}
}
}
public void setLocation(Location loc) {
//Obtener la direccion de la calle a partir de la latitud y la longitud
if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
try {
Geocoder geocoder = new Geocoder(requireContext(), Locale.getDefault());
List<Address> list = geocoder.getFromLocation(
loc.getLatitude(), loc.getLongitude(), 1);
if (!list.isEmpty()) {
Address DirCalle = list.get(0);
// latitud.setText("Mi direccion es: \n"
// + DirCalle.getAddressLine(0));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* Aqui empieza la Clase Localizacion */
public class Localizacion implements LocationListener {
YavieneFragment mainActivity;
public YavieneFragment getMainActivity() {
return mainActivity;
}
public void setMainActivity(YavieneFragment mainActivity) {
this.mainActivity = mainActivity;
}
@Override
public void onLocationChanged(Location loc) {
// Este metodo se ejecuta cada vez que el GPS recibe nuevas coordenadas
// debido a la deteccion de un cambio de ubicacion
latitud= loc.getLatitude();
longitud = loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
// Este metodo se ejecuta cuando el GPS es desactivado
// mensaje1.setText("GPS Desactivado");
}
@Override
public void onProviderEnabled(String provider) {
// Este metodo se ejecuta cuando el GPS es activado
// mensaje1.setText("GPS Activado");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Log.d("debug", "LocationProvider.AVAILABLE");
break;
case LocationProvider.OUT_OF_SERVICE:
Log.d("debug", "LocationProvider.OUT_OF_SERVICE");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.d("debug", "LocationProvider.TEMPORARILY_UNAVAILABLE");
break;
}
}
}
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_yaviene, container, false);
WebView webView = (WebView) v.findViewById(R.id.yaviene_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.yaviene.com/usuario/index.php?a="+getString(R.string.EMPRESA)+"&v=1");
Log.e("ya viene","http://www.yaviene.com/usuario/index.php?a="+getString(R.string.EMPRESA)+"&v=1");
return v;
}