本文整理匯總了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");
}
}