Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServiceGPS extends Service {
- String TAG =getClass().getSimpleName();
- private LocationManager mLocationManager = null;
- private static final int LOCATION_INTERVAL = 1;
- private class LocationListener implements android.location.LocationListener {
- Location mLastLocation;
- private LocationListener(String provider) {
- // Log.e(TAG, "LocationListener: " + provider);
- mLastLocation = new Location(provider);
- }
- @Override
- public void onLocationChanged(Location location) {
- GblVariabel.latitute = String.valueOf(location.getLatitude());
- GblVariabel.longitude = String.valueOf(location.getLongitude());
- Log.d(TAG, "onLocationChanged: "+GblVariabel.latitute);
- Log.d(TAG, "onLocationChanged: "+GblVariabel.longitude);
- mLastLocation.set(location);
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- // Log.e(TAG, "onStatusChanged: " + provider);
- }
- @Override
- public void onProviderEnabled(String provider) {
- // Log.e(TAG, "onProviderEnabled: " + provider);
- }
- @Override
- public void onProviderDisabled(String provider) {
- // Log.e(TAG, "onProviderDisabled: " + provider);
- }
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- LocationListener[] mLocationListerner = new LocationListener[]{
- new LocationListener(LocationManager.GPS_PROVIDER),
- new LocationListener(LocationManager.NETWORK_PROVIDER)
- };
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // Log.e(TAG, "Service on Start");
- super.onStartCommand(intent, flags, startId);
- return START_STICKY;
- }
- @Override
- public void onCreate() {
- // Log.e(TAG, "onCreate: ");
- super.onCreate();
- initializeLocationManager();
- try {
- mLocationManager.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, 1, mLocationListerner[1]
- );
- } catch (java.lang.SecurityException ex) {
- Log.e(TAG, "onCreate: ");
- } catch (IllegalArgumentException ex) {
- Log.e(TAG, "onCreate: " + ex.getMessage());
- }
- try {
- mLocationManager.requestLocationUpdates(
- LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, 0, mLocationListerner[0]
- );
- } catch (java.lang.SecurityException ex) {
- Log.e(TAG, "onCreate: " + ex.getMessage());
- } catch (IllegalArgumentException ex) {
- Log.e(TAG, "onCreate: " + ex.getMessage());
- }
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (mLocationManager != null) {
- for (int i = 0; i < mLocationListerner.length; i++) {
- try {
- mLocationManager.removeUpdates(mLocationListerner[i]);
- } catch (Exception ex) {
- Log.e(TAG, "onDestroy: Fail to remove location");
- }
- }
- }
- }
- private void initializeLocationManager() {
- // Log.e(TAG, "initializeLocationManager: Service");
- if (mLocationManager == null) {
- mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement