Example usage for android.location LocationListener LocationListener

List of usage examples for android.location LocationListener LocationListener

Introduction

In this page you can find the example usage for android.location LocationListener LocationListener.

Prototype

LocationListener

Source Link

Usage

From source file:a14n.geolocationdemo.MainActivity.java

private void requestLocationUpdates() {
    // Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            JSONObject locationAsJson = new JSONObject();
            try {
                locationAsJson.put("accuracy", location.getAccuracy());
                locationAsJson.put("provider", location.getProvider());
                locationAsJson.put("latitude", location.getLatitude());
                locationAsJson.put("longitude", location.getLongitude());
                locationAsJson.put("time", location.getTime());
            } catch (JSONException e) {
                Log.e(TAG, "JSON exception", e);
                return;
            }/*from w  w w  .j  av  a2  s  . com*/
            flutterView.sendToFlutter("locations", locationAsJson.toString(), null);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

From source file:com.poguico.palmabici.synchronizers.LocationSynchronizer.java

public LocationSynchronizer(FragmentActivity context) {
    manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    listener = new LocationListener() {

        @Override/*  www  .j a va  2s.c o m*/
        public void onLocationChanged(Location l) {
            location = l;
            updateViews();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
            if ((provider.equals(LocationManager.GPS_PROVIDER)
                    && !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
                    || (provider.equals(LocationManager.NETWORK_PROVIDER)
                            && !manager.isProviderEnabled(LocationManager.GPS_PROVIDER))) {
                location = null;
                updateViews();
            }
        }
    };

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000L, 0, listener);
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000L, 0, listener);

    if (location == null)
        location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location == null)
        location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    synchronizableElements = new ArrayList<SynchronizableElement>();
}

From source file:com.sanbo.synchronizers.LocationSynchronizer.java

public LocationSynchronizer(FragmentActivity context) {
    manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    listener = new LocationListener() {

        @Override//  w  ww  .  ja  v  a 2s .co m
        public void onLocationChanged(Location l) {
            location = l;
            updateViews();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
            if ((provider.equals(LocationManager.GPS_PROVIDER)
                    && !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
                    || (provider.equals(LocationManager.NETWORK_PROVIDER)
                            && !manager.isProviderEnabled(LocationManager.GPS_PROVIDER))) {
                location = null;
                updateViews();
            }
        }
    };

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000L, 0, listener);
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000L, 0, listener);

    if (location == null)
        location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location == null)
        location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    synchronizableElements = new ArrayList<SynchronizableActivity>();
}

From source file:com.digzdigital.eservicedriver.MixedPositionProvider.java

private void startBackupProvider() {
    Log.i(TAG, "backup provider start");
    if (backupListener == null) {

        backupListener = new LocationListener() {
            @Override/*w w w .j a va2  s.c  o  m*/
            public void onLocationChanged(Location location) {
                Log.i(TAG, "backup provider location");
                updateLocation(location);
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        };

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, period, 0, backupListener);
    }
}

From source file:upv.welcomeincoming.app.ARViewActivity.java

@SuppressLint("NewApi")
@Override/*  w w w.j a  v  a2s. c  om*/
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_architect_view);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    //Wikitude: Cambio en WebView para Android KitKat
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
    }

    // establecer AR-view
    this.architectView = (ArchitectView) this.findViewById(R.id.architectView);

    //Inicializar clave de Wikitude SDK (si se posee)
    // (en caso contrario, aparece una marca de agua en la architectView)
    final ArchitectConfig config = new ArchitectConfig("" /* license key */ );

    this.architectView.onCreate(config);

    // listener del locationProvider, maneja los cambios de localizacion
    this.locationListener = new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onLocationChanged(final Location location) {
            if (location != null) {
                // establecer ultima localizacion
                ARViewActivity.this.lastKnownLocaton = location;
                if (ARViewActivity.this.architectView != null) {
                    // chequeamos si la localizacion tiene altitud a un determinado nivel de exactitud (para invocar al metodo adecuado en la ARView)
                    if (location.hasAltitude() && location.hasAccuracy() && location.getAccuracy() < 7) {
                        ARViewActivity.this.architectView.setLocation(location.getLatitude(),
                                location.getLongitude(), location.getAltitude(), location.getAccuracy());
                    } else {
                        ARViewActivity.this.architectView.setLocation(location.getLatitude(),
                                location.getLongitude(),
                                location.hasAccuracy() ? location.getAccuracy() : 1000);
                    }
                }
            }
        }
    };

    // locationProvider
    this.locationProvider = new LocationProvider(this, this.locationListener);
}

From source file:it.unime.mobility4ckan.MySensor.java

public void registerSensor(Sensor mSensor) {
    LocationListener locationListener = new LocationListener() {
        @Override//from ww  w .ja va  2s.c  o m
        public void onLocationChanged(Location location) {
            //latitude = location.getLatitude();
            //longitude = location.getLongitude();
            currentSpeed = location.getSpeed();
            //locationText.setText(String.valueOf(latitude)+ "  " +String.valueOf(longitude)+ "  " +String.valueOf(speed));
            //isGPSReady = true;
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            //
        }

        @Override
        public void onProviderEnabled(String provider) {
            //
        }

        @Override
        public void onProviderDisabled(String provider) {
            //
        }
    };

    SensorEventListener sensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {

            switch (event.sensor.getType()) {
            case Sensor.TYPE_AMBIENT_TEMPERATURE: // Gradi Celsius (C)
                currentTemp = event.values[0];
                break;

            case Sensor.TYPE_PRESSURE:
                currentPressure = event.values[0]; // hPa o mbar
                break;

            case Sensor.TYPE_LIGHT: // lx
                currentLight = event.values[0];
                break;

            case Sensor.TYPE_ACCELEROMETER: // m/s2
                currentAcceleration[0] = event.values[0];
                currentAcceleration[1] = event.values[1];
                currentAcceleration[2] = event.values[2];
                break;

            case Sensor.TYPE_GYROSCOPE: // rad/s
                currentGyroscope[0] = event.values[0];
                currentGyroscope[1] = event.values[1];
                currentGyroscope[2] = event.values[2];
                break;

            case Sensor.TYPE_MAGNETIC_FIELD: // T
                currentMagnetic[0] = event.values[0];
                currentMagnetic[1] = event.values[1];
                currentMagnetic[2] = event.values[2];
                break;

            case Sensor.TYPE_PROXIMITY: // cm
                currentProximity = event.values[0];
                break;

            case Sensor.TYPE_ROTATION_VECTOR: // unita di misura sconosciuta
                currentRotation[0] = event.values[0];
                currentRotation[1] = event.values[1];
                currentRotation[2] = event.values[2];
                break;

            case Sensor.TYPE_GRAVITY: // m/s2
                currentGravity[0] = event.values[0];
                currentGravity[1] = event.values[1];
                currentGravity[2] = event.values[2];
                break;

            case Sensor.TYPE_LINEAR_ACCELERATION: // m/s2
                currentLinearAcceleration[0] = event.values[0];
                currentLinearAcceleration[1] = event.values[1];
                currentLinearAcceleration[2] = event.values[2];
                break;

            case Sensor.TYPE_RELATIVE_HUMIDITY: // %
                currentHumidity = event.values[0];
                break;

            default:
                break;
            }

        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

    //sensorManager.registerListener(locationListener, mSensor, LocationManager.GPS_PROVIDER);
    /*        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    //    ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;
            }*/
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    sensorManager.registerListener(sensorListener, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
}

From source file:com.mattprecious.locnotifier.LocationService.java

@Override
public void onCreate() {
    super.onCreate();

    LocationService.isRunning = true;//  w  ww. j a  va 2  s  .  c  om
    preferences = PreferenceManager.getDefaultSharedPreferences(this);

    updateRunningNotification();

    int lat = preferences.getInt("dest_lat", 0);
    int lng = preferences.getInt("dest_lng", 0);

    destination = new Location("");
    destination.setLatitude(lat / 1E6);
    destination.setLongitude(lng / 1E6);

    radius = preferences.getFloat("dest_radius", 0);

    notificationManager = (NotificationManager) getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.d(getClass().getSimpleName(), "Location changed");
            updateLocation(location);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

    if (preferences.getBoolean("use_gps", false)) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    Log.d(getClass().getSimpleName(), "Watching your location"); // creepy
}

From source file:com.net.rmopenmenu.MainActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.fragment_tabs_pager);

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();/* w  ww  . j a  v a 2s  .  co  m*/

    mViewPager = (ViewPager) findViewById(R.id.pager);

    mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    Bundle b1 = new Bundle();
    b1.putBoolean("menu", true);

    Bundle b2 = new Bundle();
    b2.putBoolean("menu", false);

    mTabsAdapter.addTab(mTabHost.newTabSpec("menu").setIndicator("Menu"), MenuFragment.class, b1);
    mTabsAdapter.addTab(mTabHost.newTabSpec("restaurant").setIndicator("Restaurant"), MenuFragment.class, b2);
    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
    }

    // Acquire a reference to the system Location Manager
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    locationListener = new LocationListener() {
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onLocationChanged(Location location) {
            SharedPreferences.Editor editor = prefs.edit();

            editor.putInt("lat", (int) (location.getLatitude() * 1000000));
            editor.putInt("lon", (int) (location.getLongitude() * 1000000));

            editor.commit();
        }
    };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

From source file:at.ac.tuwien.caa.docscan.camera.LocationHandler.java

private LocationHandler(Context context) {

    mContext = context;//from  ww w  .j  av  a  2s  .c om

    mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {

            boolean stopManager = false;

            // Called when a new location is found:
            if (isBetterLocation(location, mLocation)) {
                mLocation = location;

                if (mLocation.getAccuracy() <= MIN_ACCURACY)
                    stopManager = true;
            }
            if (System.currentTimeMillis() - mStartTime >= MAX_TIME_RUNNING)
                stopManager = true;

            if (stopManager && ActivityCompat.checkSelfPermission(mContext,
                    Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
                mLocationManager.removeUpdates(this);

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    if (ActivityCompat.checkSelfPermission(mContext,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        List<String> providers = mLocationManager.getProviders(true);
        if (providers.contains(LocationManager.NETWORK_PROVIDER))
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_TIME,
                    UPDATE_DISTANCE, locationListener);
        if (providers.contains(LocationManager.GPS_PROVIDER))
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, UPDATE_TIME, UPDATE_DISTANCE,
                    locationListener);

        mStartTime = System.currentTimeMillis();
    }

}

From source file:com.ut3.ehg.turismotepic.Augmentedreality.java

public void aumentedreality() {

    poisdb = new rc_pois(getApplicationContext());
    poisdb.open();/*from  ww  w .  java 2 s  .  co m*/
    datos = poisdb.getPois();
    datos.moveToFirst();
    while (!datos.isAfterLast()) {
        array2.clear();
        for (int i = 0; i < 12; i++) {
            array2.add(datos.getString(i));
        }
        JSONArray conv = new JSONArray(array2);

        array.add(conv.toString());
        datos.moveToNext();
    }

    this.architectView = (ArchitectView) this.findViewById(R.id.architectView);
    final StartupConfiguration config = new StartupConfiguration(this.getWikitudeSDKLicenseKey(),
            StartupConfiguration.Features.Geo, this.getCameraPosition());
    System.out.println("el resultado de config es " + config);
    this.architectView.callJavascript("newData('" + array.toString() + "');");
    try {
        this.architectView.onCreate(config);

    } catch (RuntimeException ex) {
        this.architectView = null;
        Toast.makeText(getApplicationContext(),
                "Tu dispositivo no es compatible con las funciones de realidad aumentada.", Toast.LENGTH_SHORT)
                .show();
        finish();
    }

    this.sensorAccuracyListener = this.getSensorAccuracyListener();
    this.locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onLocationChanged(final Location location) {
            // forward location updates fired by LocationProvider to architectView, you can set lat/lon from any location-strategy
            if (location != null) {
                // sore last location as member, in case it is needed somewhere (in e.g. your adjusted project)
                Augmentedreality.this.lastKnownLocaton = location;
                if (Augmentedreality.this.architectView != null) {
                    // check if location has altitude at certain accuracy level & call right architect method (the one with altitude information)
                    if (location.hasAltitude() && location.hasAccuracy() && location.getAccuracy() < 7) {
                        Augmentedreality.this.architectView.setLocation(location.getLatitude(),
                                location.getLongitude(), location.getAltitude(), location.getAccuracy());
                    } else {
                        Augmentedreality.this.architectView.setLocation(location.getLatitude(),
                                location.getLongitude(),
                                location.hasAccuracy() ? location.getAccuracy() : 1000);
                    }
                }
            }
        }
    };
    this.locationProvider = getLocationProvider(this.locationListener);
}