Search in sources :

Example 31 with LocationManager

use of android.location.LocationManager in project Space-Station-Tracker by Kiarasht.

the class Locations method Connected.

/**
     * Lets find user's location.
     */
private void Connected() {
    // Check if we have the right permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, R.string.errorPermissionLocation, Toast.LENGTH_LONG).show();
        return;
    }
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);
    Location location = null;
    for (int i = providers.size() - 1; i >= 0; i--) {
        location = locationManager.getLastKnownLocation(providers.get(i));
        if (location != null)
            break;
    }
    // If we got something back start parsing
    if (location != null) {
        String url = "https://maps.googleapis.com/maps/api/staticmap?" + "center=LAT,LNG&" + "zoom=13&" + "scale=1&" + "size=640x640&" + "markers=color:red%7CLAT,LNG&" + "key=AIzaSyAtpWPhzhbtqTgofnQhAHjiG12MmrY2AAE";
        mLatitude = String.valueOf(location.getLatitude());
        mLongitude = String.valueOf(location.getLongitude());
        url = url.replace("LAT", mLatitude);
        url = url.replace("LNG", mLongitude);
        try {
            List<Address> matches = new Geocoder(this).getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            final Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
            if (bestMatch != null) {
                String locationFormat = "";
                if (!"null".equals(bestMatch.getLocality())) {
                    locationFormat += bestMatch.getLocality() + ", ";
                }
                if (!"null".equals(bestMatch.getAdminArea())) {
                    locationFormat += bestMatch.getAdminArea() + " ";
                }
                if (!"null".equals(bestMatch.getCountryCode())) {
                    locationFormat += bestMatch.getCountryCode() + " ";
                }
                if (!"null".equals(bestMatch.getPostalCode())) {
                    locationFormat += bestMatch.getPostalCode();
                }
                mTitleView.setText(locationFormat);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Picasso.with(mActivity).load(url).into(mImageView);
        displayResults();
        displayPasses(null, null, null);
    } else {
        Toast.makeText(this, R.string.errorLocation, Toast.LENGTH_LONG).show();
    }
}
Also used : LocationManager(android.location.LocationManager) Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder) Location(android.location.Location)

Example 32 with LocationManager

use of android.location.LocationManager in project pictureapp by EyeSeeTea.

the class SurveyLocationListener method onLocationChanged.

/**
     * Listens for 1 locationChange event
     */
@Override
public void onLocationChanged(Location location) {
    Log.d(TAG, String.format("onLocationChanged idSurvey:%d location: %s", idSurvey, location.toString()));
    //Save location
    saveLocation(location);
    //No more updates
    LocationManager locationManager = (LocationManager) LocationMemory.getContext().getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(this);
}
Also used : LocationManager(android.location.LocationManager)

Example 33 with LocationManager

use of android.location.LocationManager in project wire-android by wireapp.

the class LocationFragment method isLocationServicesEnabled.

private boolean isLocationServicesEnabled() {
    if (!PermissionUtils.hasSelfPermissions(getContext(), LOCATION_PERMISSIONS)) {
        return false;
    }
    // We are creating a local locationManager here, as it's not sure we already have one
    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    if (locationManager == null) {
        return false;
    }
    boolean gpsEnabled;
    boolean netEnabled;
    try {
        gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception e) {
        gpsEnabled = false;
    }
    try {
        netEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception e) {
        netEnabled = false;
    }
    return netEnabled || gpsEnabled;
}
Also used : LocationManager(android.location.LocationManager)

Example 34 with LocationManager

use of android.location.LocationManager in project android_frameworks_base by crdroidandroid.

the class NetInitiatedActivity method sendUserResponse.

// Respond to NI Handler under GnssLocationProvider, 1 = accept, 2 = deny
private void sendUserResponse(int response) {
    if (DEBUG)
        Log.d(TAG, "sendUserResponse, response: " + response);
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.sendNiResponse(notificationId, response);
}
Also used : LocationManager(android.location.LocationManager)

Example 35 with LocationManager

use of android.location.LocationManager in project pictureapp by EyeSeeTea.

the class ADashboardActivityStrategy method prepareLocationListener.

public void prepareLocationListener(Activity activity, Survey survey) {
    SurveyLocationListener locationListener = new SurveyLocationListener(survey.getId_survey());
    LocationManager locationManager = (LocationManager) LocationMemory.getContext().getSystemService(Context.LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        Log.d(TAG, "requestLocationUpdates via NETWORK");
        if (ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.d(TAG, "requestLocationUpdates via GPS");
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    } else {
        Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (lastLocation != null) {
            Log.d(TAG, "location not available via GPS|NETWORK, last know: " + lastLocation);
            locationListener.saveLocation(lastLocation);
        } else {
            String defaultLatitude = activity.getString(R.string.GPS_LATITUDE_DEFAULT);
            String defaultLongitude = activity.getString(R.string.GPS_LONGITUDE_DEFAULT);
            Location defaultLocation = new Location(activity.getString(R.string.GPS_PROVIDER_DEFAULT));
            defaultLocation.setLatitude(Double.parseDouble(defaultLatitude));
            defaultLocation.setLongitude(Double.parseDouble(defaultLongitude));
            Log.d(TAG, "location not available via GPS|NETWORK, default: " + defaultLocation);
            locationListener.saveLocation(defaultLocation);
        }
    }
}
Also used : LocationManager(android.location.LocationManager) SurveyLocationListener(org.eyeseetea.malariacare.layout.listeners.SurveyLocationListener) Location(android.location.Location)

Aggregations

LocationManager (android.location.LocationManager)74 Location (android.location.Location)33 Bundle (android.os.Bundle)24 LocationListener (android.location.LocationListener)23 BluetoothAdapter (android.bluetooth.BluetoothAdapter)12 Criteria (android.location.Criteria)9 IntentFilter (android.content.IntentFilter)5 TrackerDataHelper (com.android.locationtracker.data.TrackerDataHelper)5 IOException (java.io.IOException)3 SuppressLint (android.annotation.SuppressLint)2 Intent (android.content.Intent)2 SensorManager (android.hardware.SensorManager)2 ConnectivityManager (android.net.ConnectivityManager)2 TelephonyManager (android.telephony.TelephonyManager)2 FacebookException (com.facebook.FacebookException)2 LatLng (com.google.android.gms.maps.model.LatLng)2 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)2 ActivityManager (android.app.ActivityManager)1 AlarmManager (android.app.AlarmManager)1 KeyguardManager (android.app.KeyguardManager)1