Include the following dependency in your build.gradle files.
// Project level build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
// App level build.gradle
dependencies {
implementation 'com.github.pankaj89:LocationHelper:1.0'
}
val locationHelper = LocationHelper(this, this)
locationHelper.makeLifeCyclerAware(this)
//Check location setting whether gps enable to get location
locationHelper.checkLocationSettings {
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
locationHelper.onActivityResult(requestCode, resultCode, data)
}
fun onCreate(){
//Service
locationHelper.bindAndStartService()
//Fetch Single Location
locationHelper.fetchSingleLocation { it->Location }
//Fetch Multiple Location
locationHelper.fetchMultipleLocation { it->Location }
}
fun onResume(){
super.onResume()
locationHelper.bindService()
}
locationHelper.startForegroundService()
BroadcastReceiver
class LocationUpdateReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val lat=LocationHelper.getLatitude(intent)
val lng=LocationHelper.getLongitude(intent)
Log.i("LOCATION_UPDATE", "Lat:${lat},Lng:${lng}")
}
}
Register BroadcastReceiver in manifest
<receiver
android:name=".LocationUpdateReceiver"
android:exported="true">
<intent-filter>
<action android:name="MASTER_ACTION_LOCATION_UPDATED" />
</intent-filter>
</receiver>
locationHelper.startForegroundService()
PermissionHelper (link)
Runtime Permission Helper (link)
Simple Adapter for RecyclerView (link)
ExoPlayer inside RecyclerView (link)
Copyright 2017 Pankaj Sharma
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.