use of android.location.LocationListener in project cloudrail-si-android-sdk by CloudRail.
the class POIResult method initLocationListener.
private void initLocationListener() {
mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
System.out.println("location changed");
if (firstRun) {
firstRun = false;
System.out.println("POIResult::lat = " + location.getLatitude());
for (int i = 0; i < poiServices.size(); i++) {
getPOIs(i, location);
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
}
use of android.location.LocationListener in project Klyph by jonathangerbaud.
the class PlacePickerActivity method onStart.
@Override
protected void onStart() {
super.onStart();
try {
Location location = null;
// Instantiate the default criteria for a location provider
Criteria criteria = new Criteria();
// Get a location manager from the system services
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Get the location provider that best matches the criteria
String bestProvider = locationManager.getBestProvider(criteria, false);
if (bestProvider != null) {
// Get the user's last known location
location = locationManager.getLastKnownLocation(bestProvider);
if (locationListener == null) {
// Set up a location listener if one is not already set
// up
// and the selected provider is enabled
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// On location updates, compare the current
// location to the desired location set in the
// place picker
float distance = location.distanceTo(placePickerFragment.getLocation());
if (distance >= LOCATION_CHANGE_THRESHOLD) {
placePickerFragment.setLocation(location);
placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS);
placePickerFragment.setSearchText(SEARCH_TEXT);
placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT);
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(LocationManager.NETWORK_PROVIDER, 1, LOCATION_CHANGE_THRESHOLD, locationListener);
}
}
if (location == null) {
// Todo : set default location the last saved location
location = SAN_FRANCISCO_LOCATION;
}
if (location != null) {
// Configure the place picker: search center, radius,
// query, and maximum results.
placePickerFragment.setLocation(location);
placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS);
placePickerFragment.setSearchText(SEARCH_TEXT);
placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT);
// Start the API call
placePickerFragment.loadData(true);
}
/*
* else { // If no location found, show an error
* onError(getResources().getString(R.string.no_location_error),
* true); }
*/
} catch (Exception ex) {
onError(ex);
}
}
use of android.location.LocationListener in project coursera-android by aporter.
the class LocationGetLocationActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAccuracyView = (TextView) findViewById(R.id.accuracy_view);
mTimeView = (TextView) findViewById(R.id.time_view);
mLatView = (TextView) findViewById(R.id.lat_view);
mLngView = (TextView) findViewById(R.id.lng_view);
// Acquire reference to the LocationManager
if (null == (mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE)))
finish();
// Get best last location measurement
mBestReading = bestLastKnownLocation(MIN_LAST_READ_ACCURACY, FIVE_MIN);
// Display last reading information
if (null != mBestReading) {
updateDisplay(mBestReading);
} else {
mAccuracyView.setText("No Initial Reading Available");
}
mLocationListener = new LocationListener() {
// Called back when location changes
public void onLocationChanged(Location location) {
ensureColor();
if (null == mBestReading || location.getAccuracy() < mBestReading.getAccuracy()) {
// Update best estimate
mBestReading = location;
// Update display
updateDisplay(location);
if (mBestReading.getAccuracy() < MIN_ACCURACY)
mLocationManager.removeUpdates(mLocationListener);
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// NA
}
public void onProviderEnabled(String provider) {
// NA
}
public void onProviderDisabled(String provider) {
// NA
}
};
}
use of android.location.LocationListener in project facebook-android-sdk by facebook.
the class PickerActivity method onStart.
@Override
@SuppressLint("MissingPermission")
protected void onStart() {
super.onStart();
if (FRIEND_PICKER.equals(getIntent().getData())) {
try {
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 && checkForLocationPermissionsAndRequest()) {
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) {
placePickerFragment.setLocation(location);
placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS);
placePickerFragment.setSearchText(SEARCH_TEXT);
placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT);
placePickerFragment.loadData(false);
}
} catch (Exception ex) {
onError(ex);
}
}
}
use of android.location.LocationListener in project Gadgetbridge by Freeyourgadget.
the class SettingsActivity method onPostCreate.
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Preference pref = findPreference("notifications_generic");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(enableIntent);
return true;
}
});
pref = findPreference("pref_key_miband");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent enableIntent = new Intent(SettingsActivity.this, MiBandPreferencesActivity.class);
startActivity(enableIntent);
return true;
}
});
pref = findPreference("pref_key_blacklist");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent enableIntent = new Intent(SettingsActivity.this, AppBlacklistActivity.class);
startActivity(enableIntent);
return true;
}
});
pref = findPreference("pebble_emu_addr");
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent);
preference.setSummary(newVal.toString());
return true;
}
});
pref = findPreference("pebble_emu_port");
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent);
preference.setSummary(newVal.toString());
return true;
}
});
pref = findPreference("log_to_file");
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
boolean doEnable = Boolean.TRUE.equals(newVal);
try {
if (doEnable) {
// ensures that it is created
FileUtils.getExternalFilesDir();
}
GBApplication.setupLogging(doEnable);
} catch (IOException ex) {
GB.toast(getApplicationContext(), getString(R.string.error_creating_directory_for_logfiles, ex.getLocalizedMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
}
return true;
}
});
if (!GBApplication.isRunningMarshmallowOrLater()) {
pref = findPreference("notification_filter");
PreferenceCategory category = (PreferenceCategory) findPreference("pref_key_notifications");
category.removePreference(pref);
}
pref = findPreference("location_aquire");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SettingsActivity.this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, 0);
}
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
if (provider != null) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
setLocationPreferences(location);
} else {
locationManager.requestSingleUpdate(provider, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
setLocationPreferences(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
LOG.info("provider status changed to " + status + " (" + provider + ")");
}
@Override
public void onProviderEnabled(String provider) {
LOG.info("provider enabled (" + provider + ")");
}
@Override
public void onProviderDisabled(String provider) {
LOG.info("provider disabled (" + provider + ")");
GB.toast(SettingsActivity.this, getString(R.string.toast_enable_networklocationprovider), 3000, 0);
}
}, null);
}
} else {
LOG.warn("No location provider found, did you deny location permission?");
}
return true;
}
});
pref = findPreference("canned_messages_dismisscall_send");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Prefs prefs = GBApplication.getPrefs();
ArrayList<String> messages = new ArrayList<>();
for (int i = 1; i <= 16; i++) {
String message = prefs.getString("canned_message_dismisscall_" + i, null);
if (message != null && !message.equals("")) {
messages.add(message);
}
}
CannedMessagesSpec cannedMessagesSpec = new CannedMessagesSpec();
cannedMessagesSpec.type = CannedMessagesSpec.TYPE_MISSEDCALLS;
cannedMessagesSpec.cannedMessages = messages.toArray(new String[messages.size()]);
GBApplication.deviceService().onSetCannedMessages(cannedMessagesSpec);
return true;
}
});
// Get all receivers of Media Buttons
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
PackageManager pm = getPackageManager();
List<ResolveInfo> mediaReceivers = pm.queryBroadcastReceivers(mediaButtonIntent, PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RESOLVED_FILTER);
CharSequence[] newEntries = new CharSequence[mediaReceivers.size() + 1];
CharSequence[] newValues = new CharSequence[mediaReceivers.size() + 1];
newEntries[0] = getString(R.string.pref_default);
newValues[0] = "default";
int i = 1;
for (ResolveInfo resolveInfo : mediaReceivers) {
newEntries[i] = resolveInfo.activityInfo.loadLabel(pm);
newValues[i] = resolveInfo.activityInfo.packageName;
i++;
}
final ListPreference audioPlayer = (ListPreference) findPreference("audio_player");
audioPlayer.setEntries(newEntries);
audioPlayer.setEntryValues(newValues);
audioPlayer.setDefaultValue(newValues[0]);
}
Aggregations