yayaa / LocationManager

Simplify getting user's location for Android
806 stars 187 forks source link

How disable Google Play and use only wifi and GPS? #38

Closed no-cat-no closed 7 years ago

no-cat-no commented 7 years ago

I'm making an app for China. But in China, Google Play Services do not work (Great Firewall). Tell me, how can I turn off location determination through Google Play Services and leave only GPS and WiFi?

Thanks!

P.S. Do you planing to add a definition through the GSM of the towers?

sheddar commented 7 years ago

Tell me, how can I turn off location determination through Google Play Services and leave only GPS and WiFi?

Assuming you are using default configuration, so:

Configurations.java

public static LocationConfiguration defaultConfiguration(@NonNull String rationalMessage, @NonNull String gpsMessage) {
        return new LocationConfiguration.Builder()
              .askForPermission(new PermissionConfiguration.Builder().rationaleMessage(rationalMessage).build())
              .useGooglePlayServices(new GooglePlayServicesConfiguration.Builder().build())
              .useDefaultProviders(new DefaultProviderConfiguration.Builder().gpsMessage(gpsMessage).build())
              .build();
    }

Just delete this line: .useGooglePlayServices(new GooglePlayServicesConfiguration.Builder().build()) and you're ready to go.

P.S. Do you planing to add a definition through the GSM of the towers?

DefaultLocationProvider.java

void getLocationByNetwork() {
        if (isNetworkProviderEnabled() && getSourceProvider().isNetworkAvailable(getContext())) {
            LogUtils.logI("Network is enabled, getting location...");
            askForLocation(LocationManager.NETWORK_PROVIDER);
        } else {
            LogUtils.logI("Network is not enabled, calling fail...");
            onLocationFailed(FailType.NETWORK_NOT_AVAILABLE);
        }
    }

If you change the conditions and keep only isNetworkProviderEnabled(), network provider will be used whether there is an available active data network or not. In a scenario when none of data networks (mobile, Wi-Fi, etc. ) is available, Android Location framework provides you with location information based on Cell Towers. The precision depends on the quality and features of the cell tower.

no-cat-no commented 7 years ago

Wow! Thanks!!!

no-cat-no commented 7 years ago

And small question:

If you do not use Google Services, the battery will be installed faster. For tracking this is critical. Could you give advice on how to reduce battery consumption? Maybe I must use services instead of activity or something like that?

I noticed that you are not using PASSIVE_PROVIDER.

sheddar commented 7 years ago

The Google Location Services API provides, in most cases, better battery performance.

Could you give advice on how to reduce battery consumption?

https://developer.android.com/guide/topics/location/strategies.html#Adjusting

I noticed that you are not using PASSIVE_PROVIDER.

https://developer.android.com/reference/android/location/LocationManager.html#PASSIVE_PROVIDER

http://stackoverflow.com/questions/5655241/what-is-the-meaning-of-android-location-locationmanager-passive-provider#answer-5655329

no-cat-no commented 7 years ago

ok, thanks.

yayaa commented 7 years ago

wow @kobasek thanks for answering :) Appreciated!

Actually removing network availability check is good idea, eventually we have timeout anyway. I'll implement it that way, hopefully soon :)

yayaa commented 7 years ago

And @no-cat-no if you need to customize the behaviour even further, you can create your own LocationProvider implementation and set it to LocationManager. Then, it will not use default ones, and since you're not using GooglePlayServices api provider anyway, that would mean for your case, only DefaultLocationProvider changes.

In that way, you can do any optimisation that you need to do, such as: using PassiveProvider.

sheddar commented 7 years ago

I've done some minor/major "fixes" to your project @yayaa If i have more time, i'll create a PR and share all of it with you.

yayaa commented 7 years ago

Nice @kobasek thanks👍 Just a few notes:

sheddar commented 7 years ago

Sure @yayaa ;) Have you considered wrapping your library with RxJava?

yayaa commented 7 years ago

I did, but because there are already some with RxJava i decided not to, but sure why not. I would do in a different module though, just not force people to depend RxJava if they are not already using. But please send me email if you are interested, let's not talk over a closed issue :) @kobasek