List of usage examples for android.location LocationManager isProviderEnabled
public boolean isProviderEnabled(String provider)
From source file:it.unipr.informatica.autobusparma.MappaFragment.java
public Location getLocation(long MIN_DISTANCE, long MIN_TIME) { Location location = null;/*from ww w. j a va 2s .c o m*/ try { LocationManager locationManager = (LocationManager) getActivity() .getSystemService(Context.LOCATION_SERVICE); // getting GPS status e network boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { if (isNetworkEnabled) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); } } } } } catch (Exception e) { e.printStackTrace(); } return location; }
From source file:com.kentli.cycletrack.RecordingActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recording);// w ww. j a v a 2 s . c o m initWidget(); if (rService == null) rService = new Intent(RecordingActivity.this, RecordingService.class); startService(rService); ServiceConnection sc = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { } public void onServiceConnected(ComponentName name, IBinder service) { IRecordService rs = (IRecordService) service; recordService = rs; int state = rs.getState(); updateUIAccordingToState(rs.getState()); if (state > RecordingService.STATE_IDLE) { if (state == RecordingService.STATE_FULL) { startActivity(new Intent(RecordingActivity.this, SaveTrip.class)); } else { if (state == RecordingService.STATE_RECORDING) { isRecording = true; initTrip(); } //PAUSE or RECORDING... recordService.setListener(RecordingActivity.this); } } else { // First run? Switch to user prefs screen if there are no prefs stored yet SharedPreferences settings = getSharedPreferences("PREFS", 0); if (settings.getAll().isEmpty()) { showWelcomeDialog(); } } RecordingActivity.this.unbindService(this); // race? this says we no longer care // Before we go to record, check GPS status final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { buildAlertMessageNoGps(); } } }; // This needs to block until the onServiceConnected (above) completes. // Thus, we can check the recording status before continuing on. bindService(rService, sc, Context.BIND_AUTO_CREATE); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); //Google Map MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); setButtonOnClickListeners(); }
From source file:org.odk.collect.android.activities.GeoShapeOsmMapActivity.java
private void upMyLocationOverlayLayers() { LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { overlayMyLocationLayers();/*from w w w . j av a2 s.c om*/ } else { showGPSDisabledAlertToUser(); } }
From source file:org.odk.collect.android.activities.GeoShapeOsmMapActivity.java
private void disableMyLocation() { LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { myLocationOverlay.setEnabled(false); myLocationOverlay.disableFollowLocation(); myLocationOverlay.disableMyLocation(); gpsStatus = false;//from w w w . ja va2 s . c o m } }
From source file:heartware.com.heartware_master.FB_PickerActivity.java
@Override protected void onStart() { super.onStart(); if (FRIEND_PICKER.equals(getIntent().getData())) { try {/*www. j av a 2s . co m*/ friendPickerFragment.loadData(false); } catch (Exception ex) { onError(ex); } } else if (PLACE_PICKER.equals(getIntent().getData())) { try { Location location = null; Criteria criteria = new Criteria(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String bestProvider = locationManager.getBestProvider(criteria, false); if (bestProvider != null) { location = locationManager.getLastKnownLocation(bestProvider); if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) { locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { boolean updateLocation = true; Location prevLocation = placePickerFragment.getLocation(); if (prevLocation != null) { updateLocation = location.distanceTo(prevLocation) >= LOCATION_CHANGE_THRESHOLD; } if (updateLocation) { placePickerFragment.setLocation(location); placePickerFragment.loadData(true); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD, locationListener, Looper.getMainLooper()); } } if (location == null) { String model = Build.MODEL; if (model.equals("sdk") || model.equals("google_sdk") || model.contains("x86")) { // this may be the emulator, pretend we're in an exotic place location = TEMPE_AZ_LOCATION; } location = TEMPE_AZ_LOCATION; // @TODO HARDCODED location } if (location != null) { location = TEMPE_AZ_LOCATION; placePickerFragment.setLocation(location); placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS); placePickerFragment.setSearchText(SEARCH_TEXT); placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT); placePickerFragment.loadData(false); } else { onError(getResources().getString(R.string.no_location_error), true); } } catch (Exception ex) { onError(ex); } } }
From source file:com.bangz.smartmute.services.LocationMuteService.java
private boolean addGeofences(List<Geofence> geofences) { ConnectionResult connresult = mGoogleApiClient.blockingConnect(); if (connresult.isSuccess() == false) { LogUtils.LOGE(TAG, "GoogleApiClient.blockingConnect failed! message: " + connresult.toString()); return false; }//from w w w . j a v a 2s. co m GeofencingRequest gR = new GeofencingRequest.Builder().addGeofences(geofences) .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL | GeofencingRequest.INITIAL_TRIGGER_EXIT) .build(); PendingIntent pi = getGeofencesPendingIntent(this); Status rs = LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, gR, pi).await(); boolean bret = false; if (rs.isSuccess()) { LogUtils.LOGD(TAG, "Add Geofence successful."); PrefUtils.Geofencing(this, true); ReceUtils.enableReceiver(this, LocationProviderChangedReceiver.class, false); bret = true; } else { int errcode = rs.getStatusCode(); bret = false; LogUtils.LOGD(TAG, "Add Geofence failed! errorcode = " + errcode + "Message:" + rs.getStatusMessage()); if (errcode == GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE) { LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { LogUtils.LOGD(TAG, "GPS or Network Provider is enabled. " + "Notification User to try add again late?"); } else { ReceUtils.enableReceiver(this, LocationProviderChangedReceiver.class, true); } NotificationUserFailed(); } } mGoogleApiClient.disconnect(); return bret; }
From source file:com.remdo.app.MainActivity.java
private Boolean isGPSorNetworkEnabled() { Boolean enabled = false;//from www . j av a 2s. c o m //Accedemos a los servicios de localizacin del sistema LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE); //Obtenemos el estado del GPS y de la red Boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); Boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (isGPSEnabled || isNetworkEnabled) enabled = true; return enabled; }
From source file:at.ac.uniklu.mobile.sportal.service.MutingService.java
private void mute(int alarmId) { Log.d(TAG, "mute()"); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); if (Preferences.getLocationStatus(preferences) == Preferences.LOCATION_STATUS_WAITING) { Log.d(TAG, "mute() blocked - waiting for a location update"); return;//www .ja v a 2 s . co m } // check if phone is already muted by the user AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); boolean isPhoneAlreadyMuted = audioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL && !Preferences.isMutingPeriod(preferences); if (isPhoneAlreadyMuted) { Log.d(TAG, "phone is already muted, scheduling next mute"); scheduleMute(true); return; } // load the current period from the db MutingPeriod mutingPeriod = null; Log.d(TAG, "muting period id: " + alarmId); if (DEBUG_WITH_FAKE_ALARMS) { mutingPeriod = new MutingPeriod(); mutingPeriod.setId(-1); mutingPeriod.setBegin(System.currentTimeMillis()); mutingPeriod.setEnd(mutingPeriod.getBegin() + 10000); mutingPeriod.setName("Fakeevent"); } else { mutingPeriod = Studentportal.getStudentPortalDB().mutingPeriods_getPeriod(alarmId); } // check if phone is located at university notifyUser(Studentportal.NOTIFICATION_MS_INFO, true, mutingPeriod.getName(), mutingPeriod.getName(), getString(R.string.automute_notification_course_started_locating)); boolean isPhoneLocationKnown = false; boolean isPhoneLocatedAtUniversity = false; String locationSource = null; WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); if (wifiManager.isWifiEnabled()) { ScanResult scanResult = MutingUtils.findMutingWifiNetwork(wifiManager.getScanResults()); if (scanResult != null) { Log.d(TAG, "phone located by wifi: " + scanResult.SSID); isPhoneLocationKnown = true; isPhoneLocatedAtUniversity = true; locationSource = "wifi (" + scanResult.SSID + ")"; } } if (!isPhoneLocationKnown) { // phone location could not be determined by wifi, trying network location instead... LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { Intent locationIntent = new Intent("at.ac.uniklu.mobile.sportal.LOCATION_UPDATE") .putExtra(EXTRA_ALARM_ID, alarmId); PendingIntent pendingLocationIntent = PendingIntent.getBroadcast(this, 0, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT); // remove the location receiver (so it doesn't get registered multiple times [could also happen on overlapping mute() calls) locationManager.removeUpdates(pendingLocationIntent); if (Preferences.getLocationStatus(preferences) == Preferences.LOCATION_STATUS_RECEIVED) { isPhoneLocationKnown = true; pendingLocationIntent.cancel(); Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location == null) { Log.d(TAG, "location received but still null"); } else { MutingRegion mutingRegion = MutingUtils.findOverlappingMutingRegion(location); if (mutingRegion != null) { Log.d(TAG, "phone located by network @ " + mutingRegion.getName()); isPhoneLocatedAtUniversity = true; locationSource = "location (" + mutingRegion.getName() + ")"; } } } else { Log.d(TAG, "trying to locate the phone by network..."); // wait for a location update Preferences.setLocationStatus(preferences, Preferences.LOCATION_STATUS_WAITING); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, pendingLocationIntent); return; // exit method - it will be re-called from the location broadcast receiver on a location update } } } boolean isAlwaysMuteEnabled = Preferences.isAutomuteWithoutLocation(this, preferences); if (isPhoneLocationKnown) { if (!isPhoneLocatedAtUniversity) { Log.d(TAG, "phone is not located at university, scheduling next mute"); scheduleMute(true); removeNotification(Studentportal.NOTIFICATION_MS_INFO); return; } } else { Log.d(TAG, "phone cannot be located"); if (!isAlwaysMuteEnabled) { Log.d(TAG, "alwaysmute is disabled, scheduling next mute"); Preferences.setLocationStatus(preferences, Preferences.LOCATION_STATUS_NONE); scheduleMute(true); removeNotification(Studentportal.NOTIFICATION_MS_INFO); return; } } // only turn the ringtone off if we aren't currently in a muting period. // if we are in a muting period the ringtone is already muted and the request should be ignored, // else rintoneTurnOn() won't turn the ringtone back on because ringtone override will be set to true if (!Preferences.isMutingPeriod(preferences)) { MutingUtils.ringtoneTurnOff(this); } // persist that from now on the phone is in a muting period Preferences.setMutingPeriod(preferences, true); // inform user via a notification that a course has started and the phone has been muted notifyUser(Studentportal.NOTIFICATION_MS_INFO, true, mutingPeriod.getName(), mutingPeriod.getName(), getString(R.string.automute_notification_course_muted)); final boolean isPhoneLocationKnownAnalytics = isPhoneLocationKnown; final String locationSourceAnalytics = locationSource; Analytics.onEvent(Analytics.EVENT_MUTINGSERVICE_MUTE, "isPhoneLocationKnown", isPhoneLocationKnownAnalytics + "", "locationSource", locationSourceAnalytics); scheduleUnmute(mutingPeriod.getEnd()); }
From source file:com.uproot.trackme.LocationActivity.java
@Override protected void onStart() { super.onStart(); // Check if the GPS setting is currently enabled on the device. // This verification should be done during onStart() because the system // calls this method // when the user returns to the activity, which ensures the desired // location provider is // enabled each time the activity resumes from the stopped state. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if (!gpsEnabled) { // Build an alert dialog here that requests that the user enable // the location services, then when the user clicks the "OK" button, // call enableLocationSettings() new EnableGpsDialogFragment().show(getSupportFragmentManager(), "enableGpsDialog"); }/* w w w . j a v a2 s .c o m*/ setup(); }
From source file:net.jongrakko.zipsuri.fragment.ExpertModifyFragment.java
@Override public boolean onMyLocationButtonClick() { LocationManager mLocationManager = (LocationManager) getContext() .getSystemService(Context.LOCATION_SERVICE); if (!mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { ProgressManger.showToast(" ."); } else {//from ww w .ja v a2 s . c om ProgressManger.showToast(" ..."); } return false; }