npanasenko / LocationManager

Simplify getting user's location for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LocationManager

To get location on Android Devices, you need to

  • Check whether GPS Provider is enabled or not
  • Ask user to enable it if not
  • Check again whether user actually did enable it or not
  • If it is enabled, start location update request
  • But switch to network if GPS is not retrieving location data
  • Check whether Network Provider is enabled or not
  • If it is, start location update request
  • If none of these work, then fail

But wait, it didn't finish yet. Now we have Google Play Services optimised location provider FusedLocationProviderApi which provides a common location pool that any application use this api can retrieve location in which interval it requires. It reduces battery usage, and increases getting location period. But to implement this, you need to

  • Check whether Google Play Services is available on device
  • If not, and if user can handle it, ask user to do it
  • Then, check again to see did user actually do it or not
  • If still not, move on previous section and do all those steps
  • If user did actually handle it, then start location update request

All of these steps, just to retrieve user's current location. And in every application you build, you need to reconsider what you did and what you need to add for this time.

This library will provide you to handle all of these steps by creating a configuration object:

  • Do your optimisations,
  • Possible to change the way it works,
  • Possible to change configuration on runtime,
  • Provide your own location provider mechanism,
  • Provide different configuration objects in different activities,

This library doesn't use singleton structure, so it will be specified to activity and it requires quite a lot lifecycle information to handle all the steps between onCreate - onResume - onPause - onDestroy - onActivityResult - onRequestPermissionsResult. You can use LocationBaseActivity or you can manually call those methods in your activity.

See the sample application for detailed usage

Configuration

All those options below are optional. Use only those you really want to customize. Even though GPS Message and Rational Message are also optional, if you do not set them user will see empty dialogs because those fields do not have default values.

new LocationConfiguration()
                .keepTracking(true)
                .useOnlyGPServices(false)
                .askForGooglePlayServices(true)
                .askForSettingsApi(true)
                .failOnConnectionSuspended(true)
                .failOnSettingsApiSuspended(false)
                .doNotUseGooglePlayServices(false)
                .askForEnableGPS(true)
                .setMinAccuracy(200.0f)
                .setWithinTimePeriod(60 * 1000)
                .setTimeInterval(10 * 1000)
                .setWaitPeriod(ProviderType.GOOGLE_PLAY_SERVICES, 5 * 1000)
                .setWaitPeriod(ProviderType.GPS, 15 * 1000)
                .setWaitPeriod(ProviderType.NETWORK, 10 * 1000)
                .setLocationRequest(YourLocationRequest)
                .setGPSMessage("Would you mind to turn GPS on?")
                .setRationalMessage("Gimme the permission!");

In LocationConfiguration class, all of these methods are explained as when and why to use.

Besides these configurations you can also create your own provider which extends LocationProvider class and implement your own logic instead of using DefaultLocationProvider. When you call setLocationProvider method below on LocationManager it will pass the configuration you defined and the activity instance to your provider, so you can count on those values as well.

getLocationManager().setLocationProvider(new YourOwnLocationProvider());

Library has a lot of log implemented in it, so you can set your LogType to get how much information you need to. Suggested to use LogType.GENERAL in debug mode and LogType.NONE in release mode though. Manager has a static method to change logType configuration. As default it is set to LogType.IMPORTANT in order to display only important steps...

LocationManager.setLogType(LogType.IMPORTANT);

AndroidManifest

Below permissions are required while using NetworkProvider, and they are not in "Dangerous Permissions" categories.

<!-- Required to check whether user has network connection or not -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

But these permissions are in "Dangerous Permissions" categories, so library will ask for permission on Android M and above.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Download

Add library dependency to your build.gradle file:

dependencies {    
     compile 'com.yayandroid:LocationManager:1.1.4'
}

License

Copyright 2016 yayandroid

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Simplify getting user's location for Android


Languages

Language:Java 100.0%