diff --git a/app/src/main/java/com/bing89/travebing/MainActivity.kt b/app/src/main/java/com/bing89/travebing/MainActivity.kt index 7db75bf..c5e67fa 100644 --- a/app/src/main/java/com/bing89/travebing/MainActivity.kt +++ b/app/src/main/java/com/bing89/travebing/MainActivity.kt @@ -3,11 +3,13 @@ package com.bing89.travebing import android.Manifest +import android.Manifest.* import android.annotation.SuppressLint import android.content.ContentValues import android.content.Context import android.content.Intent import android.content.pm.PackageManager +import android.location.Criteria import android.location.Location import android.location.LocationListener import android.location.LocationManager @@ -66,7 +68,8 @@ class MainActivity : AppCompatActivity() { startMonitoring() } } - + checkPermissions() + getLocation() locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager } @@ -77,7 +80,7 @@ class MainActivity : AppCompatActivity() { // startCamera() // } takePhoto() - getLocation() + // val intervalMillis = 60000L // 间隔时间,这里设置为1分钟 // val handler = Handler(Looper.getMainLooper()) // @@ -98,26 +101,9 @@ class MainActivity : AppCompatActivity() { startButton.text = "出发" } @RequiresPermission( - anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION], + anyOf = [permission.ACCESS_COARSE_LOCATION, permission.ACCESS_FINE_LOCATION], ) - @SuppressLint("QueryPermissionsNeeded") - private fun capturePhotoAndUpload() { - if (checkPermissions()) { - // 拍照 - val photoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) - if (photoIntent.resolveActivity(packageManager) != null) { - val photoFile = createPhotoFile() - photoFilePath = photoFile?.absolutePath - val photoUri = FileProvider.getUriForFile( - this, - "com.bing89.travebing.fileprovider", - photoFile!! - ) - photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri) -// startActivityForResult(this, PHOTO_REQUEST_CODE) - } - } - } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == PHOTO_REQUEST_CODE ) { @@ -126,9 +112,9 @@ class MainActivity : AppCompatActivity() { } private fun checkPermissions(): Boolean { val requiredPermissions = arrayOf( - Manifest.permission.CAMERA, - Manifest.permission.WRITE_EXTERNAL_STORAGE, - Manifest.permission.ACCESS_FINE_LOCATION + permission.CAMERA, + permission.WRITE_EXTERNAL_STORAGE, + permission.ACCESS_FINE_LOCATION ) for (permission in requiredPermissions) { @@ -144,6 +130,15 @@ class MainActivity : AppCompatActivity() { return true } + //define the listener + private val locationListener: LocationListener = object : LocationListener { + override fun onLocationChanged(location: Location) { + updateGpsInfo(location) + } + override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {} + override fun onProviderEnabled(provider: String) {} + override fun onProviderDisabled(provider: String) {} + } private fun createPhotoFile(): File? { val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) @@ -151,41 +146,37 @@ class MainActivity : AppCompatActivity() { return File.createTempFile("JPEG_${timeStamp}_", ".jpg", storageDir) } - - private fun getLocation() { - val locationListener = object : LocationListener { - override fun onLocationChanged(location: Location) { - updateGpsInfo(location) - } - - @Deprecated("Deprecated in Java") - override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { - // Ignored - } - - override fun onProviderEnabled(provider: String) { - // Ignored - } - - override fun onProviderDisabled(provider: String) { - // Ignored - } + private fun getLocation(){ + val locMan = getSystemService(Context.LOCATION_SERVICE) as LocationManager + val checkCameraPermission = ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) + val checkCallPhonePermission = + ContextCompat.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION) + if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED || checkCameraPermission != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(this, arrayOf( + permission.ACCESS_COARSE_LOCATION, + permission.ACCESS_FINE_LOCATION + ), 22) } - - if (ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_FINE_LOCATION - ) == PackageManager.PERMISSION_GRANTED - ) { - locationManager?.requestLocationUpdates( - LocationManager.GPS_PROVIDER, - 0, - 0f, - locationListener - ) + try { + // Request location updates + val providers = locMan.getProviders(false) + var provider = LocationManager.NETWORK_PROVIDER + if(providers.size != 0) { + if (providers.contains(LocationManager.GPS_PROVIDER)) { + provider = LocationManager.GPS_PROVIDER + }else if (providers.contains(LocationManager.PASSIVE_PROVIDER)){ + provider = LocationManager.PASSIVE_PROVIDER + } + } + Toast.makeText(baseContext, "使用" + provider + "定位", Toast.LENGTH_SHORT).show() + Log.d(TAG, "==================>>>> get location from gps") + locMan.requestLocationUpdates(provider, 10000L, 100f, locationListener) + } catch(ex: SecurityException) { + Log.d(TAG, "Security Exception, no location available") } } + private fun updateGpsInfo(location: Location) { val latitude = location.latitude val longitude = location.longitude diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 944f30f..4e2a9db 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -63,14 +63,14 @@ app:layout_constraintTop_toTopOf="parent" />