Can I use LocationListener with LocationManager in fragment? Actually when I use it, it gives me error in this line:
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, (android.location.LocationListener) this);
and when I do not casts the 4th parameter to android.location.LocationListener
it gives me error...
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Speedometer extends Fragment implements LocationListener {
TextView txt;
public Speedometer(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.speedometer, container, false);
txt=(TextView)rootView.findViewById(R.id.speedometer);
LocationManager lm= (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, (android.location.LocationListener) this);
return rootView;
}
@Override
public void onLocationChanged(Location location) {
if(location==null){
txt.setText("-.- m/s");
} else {
float nCurrentSpeed=location.getSpeed();
txt.setText(nCurrentSpeed+"m/s");
}
}
}
I think you have mistakenly implemented com.google.android.gms.location.LocationListener
interface that has just one 1 abstract method onLocationChanged
You should rather implement android.location.LocationListener
that has 4 abstract methods
onLocationChanged(Location location)
,
onProviderDisabled(String provider)
,
onProviderEnabled(String provider)
,
onStatusChanged(String provider, int status, Bundle extras)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With