Created
May 28, 2019 16:05
-
-
Save oligazar/01c798a9fdaa8b1893c95d0bb340d34c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LocationReceiver : BroadcastReceiver() { | |
| override fun onReceive(context: Context?, intent: Intent?) { | |
| val action: String? = intent?.action | |
| if (ACTION_PROCESS_UPDATE == action) { | |
| val result: LocationResult? = LocationResult.extractResult(intent) | |
| val location: Location? = result?.lastLocation | |
| val locationString = "${location?.latitude}/${location?.longitude}" | |
| try { | |
| MainActivity.instance?.updateTextView(locationString) | |
| } catch (e: Exception) { | |
| Toast.makeText(context, "$e, locationString", Toast.LENGTH_SHORT).show() | |
| } | |
| } | |
| } | |
| companion object { | |
| const val ACTION_PROCESS_UPDATE = "com.agforce.gps.UPDATE_LOCATION" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainActivity : AppCompatActivity() { | |
| lateinit var fusedClient: FusedLocationProviderClient | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| // this is awful! | |
| instance = this | |
| Dexter.withActivity(this) | |
| .withPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) | |
| .withListener(object: PermissionListener{ | |
| override fun onPermissionGranted(response: PermissionGrantedResponse?) { | |
| updateLocation() | |
| } | |
| override fun onPermissionRationaleShouldBeShown( | |
| permission: PermissionRequest?, | |
| token: PermissionToken? | |
| ) { | |
| Toast.makeText(this@MainActivity, "Please grand location permission", Toast.LENGTH_SHORT).show() | |
| } | |
| override fun onPermissionDenied(response: PermissionDeniedResponse?) { | |
| Toast.makeText(this@MainActivity, "Please grand location permission", Toast.LENGTH_SHORT).show() | |
| } | |
| }).check() | |
| } | |
| private fun updateLocation() { | |
| fusedClient = LocationServices.getFusedLocationProviderClient(this) | |
| if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
| return | |
| } | |
| fusedClient.requestLocationUpdates(buildLocationRequest(), getPendingIntent()) | |
| } | |
| private fun getPendingIntent(): PendingIntent? { | |
| val intent = Intent(this, LocationReceiver::class.java) | |
| intent.action = LocationReceiver.ACTION_PROCESS_UPDATE | |
| return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) | |
| } | |
| private fun buildLocationRequest() = LocationRequest().apply { | |
| priority = LocationRequest.PRIORITY_HIGH_ACCURACY | |
| interval = UPDATES_INTERVAL | |
| fastestInterval = FASTEST_INTERVAL | |
| smallestDisplacement = SMALLEST_DISPLACEMENT | |
| } | |
| fun updateTextView(locationString: String) { | |
| } | |
| companion object { | |
| const val UPDATES_INTERVAL = 5 * 1000L | |
| const val FASTEST_INTERVAL = 2 * 1000L | |
| const val SMALLEST_DISPLACEMENT = 10f | |
| var instance: MainActivity? = null | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment