本文整理汇总了Java中android.location.LocationListener类的典型用法代码示例。如果您正苦于以下问题:Java LocationListener类的具体用法?Java LocationListener怎么用?Java LocationListener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LocationListener类属于android.location包,在下文中一共展示了LocationListener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onDestroy
import android.location.LocationListener; //导入依赖的package包/类
/**
* Location listeners are removed from manager before the service is destroyed
*/
@Override
public void onDestroy(){
if (!isAdded){
addGraphToDB();
Log.d(TAG, "Added graph before destroying service");
}
Log.d(TAG, "Entered onDestroy");
if (manager != null){
for (LocationListener listener : locationListeners) {
manager.removeUpdates(listener);
}
}
super.onDestroy();
}
示例2: initGps
import android.location.LocationListener; //导入依赖的package包/类
private void initGps() {
if (mLocationManager != null) {
List<String> providers = mLocationManager.getProviders(true);
mLocationListeners = new ArrayList<>(providers.size());
for (String provider : providers) {
LocationListener listener = new MyLocationListener();
mLocationListeners.add(listener);
mLocationManager.requestLocationUpdates(provider, 2000, 5f, listener);
if (null == mLocation) {
mLocation = mLocationManager.getLastKnownLocation(provider);
}
}
updateUI(mLocation);
}
}
示例3: register
import android.location.LocationListener; //导入依赖的package包/类
public void register(final LocationListener client) {
timer.schedule(new TimerTask() {
@Override
public void run() {
activity.runOnUiThread(new Runnable(){
@Override
public void run() {
dLat += (360 * 10)/(2 * Math.PI * R);
Location loc = new Location(start);
loc.setLatitude(start.getLatitude() - dLat);
loc.setLongitude(start.getLongitude());
client.onLocationChanged(loc);
Log.i(TAG, "Moved");
}
});
}
}, 0, 1000);
}
示例4: onDeRegistered
import android.location.LocationListener; //导入依赖的package包/类
@Override
public void onDeRegistered()
{
if(this.mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
if(Build.VERSION.SDK_INT >= 23 && !(ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
for(LocationListener ll : this.locationListeners) {
this.locationManager.removeUpdates(ll);
}
}
this.timer.cancel();
this.map = null;
this.lastKnownLocation = null;
}
示例5: ShadowLocationManager
import android.location.LocationListener; //导入依赖的package包/类
@Test
@Ignore
/*
It appears there is a bug in the ShadowLocationManager. When updates are removed there is a
null check against the map of of listeners. Removing updates does not null the list of providers;
therefore when listeners are added there are not added to the map. See line 228 of ShadowLocationManager (Robolectric 3.0-rc2)
*/
public void whenGPSUpdatesAreNoLongerRequiredThenTheGPSListenerIsRemovedFromTheLocationManager() {
groceryStoreManager.listenForLocationUpdates(true);
List<LocationListener> locationListeners = shadowLocationManager.getRequestLocationUpdateListeners();
assertTrue(shadowLocationManager.getProvidersForListener(locationListeners.get(0)).contains(LocationManager.GPS_PROVIDER));
groceryStoreManager.removeGPSListener();
locationListeners = shadowLocationManager.getRequestLocationUpdateListeners();
assertFalse(shadowLocationManager.getProvidersForListener(locationListeners.get(0)).contains(LocationManager.GPS_PROVIDER));
}
示例6: itShouldNotProvideUpdateIfAnyProviderIsAvailable
import android.location.LocationListener; //导入依赖的package包/类
@Test
public void itShouldNotProvideUpdateIfAnyProviderIsAvailable() {
// given
given(mockedLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
.willReturn(false);
given(mockedLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
.willReturn(false);
LocationListener listener = mock(LocationListener.class);
// when
locationTracker.startListener(listener);
// then
verify(mockedLocationManager, never()).requestLocationUpdates(
eq(LocationManager.GPS_PROVIDER),
anyLong(),
anyFloat(),
eq(listener));
}
示例7: itShouldProvideUpdatesIfAtLeastGPSProviderIsAvailable
import android.location.LocationListener; //导入依赖的package包/类
@Test
public void itShouldProvideUpdatesIfAtLeastGPSProviderIsAvailable() {
// given
given(mockedLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
.willReturn(true);
given(mockedLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
.willReturn(false);
LocationListener listener = mock(LocationListener.class);
// when
locationTracker.startListener(listener);
// then
verify(mockedLocationManager).requestLocationUpdates(
eq(LocationManager.GPS_PROVIDER),
anyLong(),
anyFloat(),
eq(listener));
}
示例8: itShouldProvideUpdatesIfAtLeastNetworkProviderIsAvailable
import android.location.LocationListener; //导入依赖的package包/类
@Test
public void itShouldProvideUpdatesIfAtLeastNetworkProviderIsAvailable() {
// given
given(mockedLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
.willReturn(false);
given(mockedLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
.willReturn(true);
LocationListener listener = mock(LocationListener.class);
// when
locationTracker.startListener(listener);
// then
verify(mockedLocationManager)
.requestLocationUpdates(
eq(LocationManager.NETWORK_PROVIDER),
anyLong(),
anyFloat(),
eq(listener));
}
示例9: itShouldReturnNullFromLastKnowLocationIfAnyProviderIsAvailable
import android.location.LocationListener; //导入依赖的package包/类
@Test
public void itShouldReturnNullFromLastKnowLocationIfAnyProviderIsAvailable() {
// given
List<String> providers =
Lists.newArrayList(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER);
given(mockedLocationManager.getProviders(true))
.willReturn(providers);
given(mockedLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
.willReturn(false);
given(mockedLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
.willReturn(false);
LocationListener listener = mock(LocationListener.class);
// when
Location result = locationTracker.getLastKnowLocation();
// then
assertNull(result);
}
示例10: itShouldReturnLocationFromLastKnowLocationIfAtLeastGPSProviderIsAvailable
import android.location.LocationListener; //导入依赖的package包/类
@Test
public void itShouldReturnLocationFromLastKnowLocationIfAtLeastGPSProviderIsAvailable() {
// given
Location expected = mock(Location.class);
List<String> providers =
Lists.newArrayList(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER);
given(mockedLocationManager.getProviders(true))
.willReturn(providers);
given(mockedLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
.willReturn(true);
given(mockedLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
.willReturn(false);
given(mockedLocationManager.getLastKnownLocation(anyString()))
.willReturn(expected);
LocationListener listener = mock(LocationListener.class);
// when
Location result = locationTracker.getLastKnowLocation();
// then
assertEquals(expected, result);
}
示例11: itShouldReturnLocationFromLastKnowLocationIfAtLeastNetworkProviderIsAvailable
import android.location.LocationListener; //导入依赖的package包/类
@Test
public void itShouldReturnLocationFromLastKnowLocationIfAtLeastNetworkProviderIsAvailable() {
// given
Location expected = mock(Location.class);
List<String> providers =
Lists.newArrayList(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER);
given(mockedLocationManager.getProviders(true))
.willReturn(providers);
given(mockedLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
.willReturn(false);
given(mockedLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
.willReturn(true);
given(mockedLocationManager.getLastKnownLocation(anyString()))
.willReturn(expected);
LocationListener listener = mock(LocationListener.class);
// when
Location result = locationTracker.getLastKnowLocation();
// then
assertEquals(expected, result);
}
示例12: find
import android.location.LocationListener; //导入依赖的package包/类
public Observable<Coordinate> find() {
return Observable.create(new Observable.OnSubscribe<Coordinate>() {
@Override
public void call(Subscriber<? super Coordinate> subscriber) {
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
final String provider = mManager.getBestProvider(criteria, true);
final LocationListener listener = new InnerLocationListener(subscriber);
final Looper currentLooper = Looper.myLooper();
if (currentLooper == null) {
Looper.prepare();
mManager.requestSingleUpdate(provider, listener, Looper.myLooper());
Looper.loop();
} else {
mManager.requestSingleUpdate(provider, listener, currentLooper);
}
}
});
}
示例13: getLocationObserver
import android.location.LocationListener; //导入依赖的package包/类
public Observable<Location> getLocationObserver() {
return Observable.create(new ObservableOnSubscribe<Location>() {
@Override
public void subscribe(final ObservableEmitter<Location> e) throws Exception {
LocationListener listener = new LocationListenerWithDefaults() {
@Override
public void onLocationChanged(Location location) {
e.onNext(location);
}
};
if (ActivityCompat.checkSelfPermission(activityContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(activityContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activityContext, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
});
}
示例14: unregisterLocationService
import android.location.LocationListener; //导入依赖的package包/类
/**
* To un-register the Location Service
*/
public final void unregisterLocationService(LocationListener locationListener) throws LocationServiceException
{
if(gps!=null)
{
if(enableDebugging)
Log.d(TAG,"Unregister Location Service");
gps.stopUsingGPS(locationListener); // Change the Listener
CAFConfig.setSensorLocation(false);
}
else
{
Log.d(TAG,"gps is null");
}
}
示例15: unregisterLocationService
import android.location.LocationListener; //导入依赖的package包/类
/**
* To un-register the Location Service
*/
public final void unregisterLocationService(LocationListener locationListener) throws LocationServiceException
{
if(locationDataListener!=null)
{
if(enableDebugging)
Log.d(TAG,"Unregister Location Service");
locationDataListener.unregisterLocationListener(locationListener); // Change the Listener
CAFConfig.setSensorLocation(false);
}
else
{
Log.d(TAG,"locationDataListener is null");
}
}