Last active
August 2, 2020 00:54
-
-
Save piumimaheshika/7dd9758c4e0bd36d615d83ba233e3268 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
| package com.example.piumi.location | |
| import android.Manifest | |
| import android.content.Context | |
| import android.content.Intent | |
| import android.content.pm.PackageManager | |
| import android.location.* | |
| import android.os.* | |
| import android.provider.Settings | |
| import android.support.v4.content.ContextCompat | |
| import android.support.v7.app.AppCompatActivity | |
| import android.util.Log | |
| import kotlinx.android.synthetic.main.activity_main.* | |
| private const val REQUEST_CODE: Int = 101 | |
| class MainActivity : AppCompatActivity() { | |
| private lateinit var mLocationManager: LocationManager | |
| private var mLocationListener = object : LocationListener { | |
| override fun onLocationChanged(location: Location?) { | |
| text_location.text = location?.latitude.toString() + " " + location?.longitude.toString() | |
| } | |
| override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { | |
| Log.d("LOCATION", " Status Changed ") | |
| } | |
| override fun onProviderEnabled(provider: String?) { | |
| Log.d("LOCATION", " Provider Enabled") | |
| } | |
| override fun onProviderDisabled(provider: String?) { | |
| var intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS) | |
| startActivity(intent) | |
| } | |
| } | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || | |
| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { | |
| requestPermissions(arrayOf<String>( | |
| Manifest.permission.ACCESS_FINE_LOCATION, | |
| Manifest.permission.ACCESS_COARSE_LOCATION, | |
| Manifest.permission.INTERNET), | |
| REQUEST_CODE) | |
| return | |
| } else { | |
| requestLocation() | |
| } | |
| } | |
| } | |
| private fun requestLocation() { | |
| button_location.setOnClickListener { | |
| var criteria = Criteria() | |
| criteria.setAccuracy(Criteria.ACCURACY_COARSE); | |
| criteria.setAltitudeRequired(false); | |
| criteria.setBearingRequired(false); | |
| criteria.setCostAllowed(true); | |
| criteria.setPowerRequirement(Criteria.POWER_LOW); | |
| var provider = mLocationManager.getBestProvider(criteria, true) | |
| Log.d("LOCATION", "best Provider " + provider) | |
| mLocationManager.requestLocationUpdates(provider, 1000, 0f, mLocationListener) | |
| } | |
| } | |
| override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { | |
| when (requestCode) { | |
| REQUEST_CODE -> | |
| if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
| requestLocation() | |
| return | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment