-
-
Save paulobunga/359a7a6e1abb353308394dfd1521c3d2 to your computer and use it in GitHub Desktop.
Fragment for Location Manager
This file contains 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(Activity activity) { | |
super.onAttach(activity); | |
mLocationManager = (LocationManager) activity.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 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment