feat 可以定位了
This commit is contained in:
parent
1707daa21d
commit
5f7c021712
|
@ -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,40 +146,36 @@ class MainActivity : AppCompatActivity() {
|
|||
return File.createTempFile("JPEG_${timeStamp}_", ".jpg", storageDir)
|
||||
}
|
||||
|
||||
|
||||
private fun getLocation() {
|
||||
val locationListener = object : LocationListener {
|
||||
override fun onLocationChanged(location: Location) {
|
||||
updateGpsInfo(location)
|
||||
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)
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
|
||||
// Ignored
|
||||
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
|
||||
}
|
||||
|
||||
override fun onProviderEnabled(provider: String) {
|
||||
// Ignored
|
||||
}
|
||||
|
||||
override fun onProviderDisabled(provider: String) {
|
||||
// Ignored
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
val latitude = location.latitude
|
||||
|
|
|
@ -63,14 +63,14 @@
|
|||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/filePaths"
|
||||
android:id="@+id/locationView"
|
||||
android:layout_width="376dp"
|
||||
android:layout_height="37dp"
|
||||
android:layout_marginStart="19dp"
|
||||
android:layout_marginTop="636dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginBottom="58dp"
|
||||
android:text="TextView"
|
||||
android:visibility="invisible"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
|
Loading…
Reference in New Issue