Last active
September 28, 2021 20:53
-
-
Save louisbl/770c0a2c636865a9025b to your computer and use it in GitHub Desktop.
Fragment for Location Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
public class LocationFragment extends Fragment implements LocationListener { | |
private static final String TAG = "LocationFragment"; | |
private LocationManager mLocationManager; | |
public LocationFragment() { | |
} | |
@Override | |
public void onAttach(Context context) { | |
super.onAttach(context); | |
mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
Log.i(TAG, "onResume"); | |
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); | |
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
Log.i(TAG, "onPause"); | |
mLocationManager.removeUpdates(this); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
Log.i(TAG, String.valueOf(location.getLatitude())); | |
Log.i(TAG, String.valueOf(location.getLongitude())); | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
Log.i(TAG, "Provider " + provider + " has now status: " + status); | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
Log.i(TAG, "Provider " + provider + " is enabled"); | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
Log.i(TAG, "Provider " + provider + " is disabled"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocationFragment locaFrag = new LocationFragment(); | |
getSupportFragmentManager() | |
.beginTransaction() | |
.add(locaFrag, "locaFrag") | |
.commit(); |
Helped my badly! Thanks.
Working great.
The only change I made is
@Override
public void onAttach(Context context) {
super.onAttach(context);
mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
}
Because the onAttach that you suggested is deprecated in androidx
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bro... This code really helped me... Tanx a million