feat 可以定位了

This commit is contained in:
huangkun 2024-01-24 18:34:25 +08:00
parent 1707daa21d
commit 5f7c021712
2 changed files with 48 additions and 57 deletions

View File

@ -3,11 +3,13 @@
package com.bing89.travebing package com.bing89.travebing
import android.Manifest import android.Manifest
import android.Manifest.*
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.location.Criteria
import android.location.Location import android.location.Location
import android.location.LocationListener import android.location.LocationListener
import android.location.LocationManager import android.location.LocationManager
@ -66,7 +68,8 @@ class MainActivity : AppCompatActivity() {
startMonitoring() startMonitoring()
} }
} }
checkPermissions()
getLocation()
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
} }
@ -77,7 +80,7 @@ class MainActivity : AppCompatActivity() {
// startCamera() // startCamera()
// } // }
takePhoto() takePhoto()
getLocation()
// val intervalMillis = 60000L // 间隔时间这里设置为1分钟 // val intervalMillis = 60000L // 间隔时间这里设置为1分钟
// val handler = Handler(Looper.getMainLooper()) // val handler = Handler(Looper.getMainLooper())
// //
@ -98,26 +101,9 @@ class MainActivity : AppCompatActivity() {
startButton.text = "出发" startButton.text = "出发"
} }
@RequiresPermission( @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?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
if (resultCode == PHOTO_REQUEST_CODE ) { if (resultCode == PHOTO_REQUEST_CODE ) {
@ -126,9 +112,9 @@ class MainActivity : AppCompatActivity() {
} }
private fun checkPermissions(): Boolean { private fun checkPermissions(): Boolean {
val requiredPermissions = arrayOf( val requiredPermissions = arrayOf(
Manifest.permission.CAMERA, permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE, permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION permission.ACCESS_FINE_LOCATION
) )
for (permission in requiredPermissions) { for (permission in requiredPermissions) {
@ -144,6 +130,15 @@ class MainActivity : AppCompatActivity() {
return true 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? { private fun createPhotoFile(): File? {
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
@ -151,40 +146,36 @@ class MainActivity : AppCompatActivity() {
return File.createTempFile("JPEG_${timeStamp}_", ".jpg", storageDir) return File.createTempFile("JPEG_${timeStamp}_", ".jpg", storageDir)
} }
private fun getLocation(){ private fun getLocation(){
val locationListener = object : LocationListener { val locMan = getSystemService(Context.LOCATION_SERVICE) as LocationManager
override fun onLocationChanged(location: Location) { val checkCameraPermission = ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION)
updateGpsInfo(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)
} }
try {
@Deprecated("Deprecated in Java") // Request location updates
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { val providers = locMan.getProviders(false)
// Ignored 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
} }
override fun onProviderEnabled(provider: String) {
// Ignored
} }
Toast.makeText(baseContext, "使用" + provider + "定位", Toast.LENGTH_SHORT).show()
override fun onProviderDisabled(provider: String) { Log.d(TAG, "==================>>>> get location from gps")
// Ignored locMan.requestLocationUpdates(provider, 10000L, 100f, locationListener)
} catch(ex: SecurityException) {
Log.d(TAG, "Security Exception, no location available")
} }
} }
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
locationManager?.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0f,
locationListener
)
}
}
private fun updateGpsInfo(location: Location) { private fun updateGpsInfo(location: Location) {
val latitude = location.latitude val latitude = location.latitude

View File

@ -63,14 +63,14 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<TextView <TextView
android:id="@+id/filePaths" android:id="@+id/locationView"
android:layout_width="376dp" android:layout_width="376dp"
android:layout_height="37dp" android:layout_height="37dp"
android:layout_marginStart="19dp" android:layout_marginStart="19dp"
android:layout_marginTop="636dp" android:layout_marginTop="636dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:layout_marginBottom="58dp" android:layout_marginBottom="58dp"
android:text="TextView" android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"