//package your.package.name
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import android.Manifest
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.net.Uri
import android.os.Bundle
import android.os.Looper
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import android.provider.Settings
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority
class MainActivity : AppCompatActivity() {
private lateinit var locationCallback: LocationCallback
private lateinit var locationRequest: LocationRequest
private lateinit var fusedLocationClient: FusedLocationProviderClient
private var requestingLocationUpdates = false
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
//⑦ ユーザーが権限を許可したか
if (isGranted) {
//⑧a 制限された機能にアクセスする
requestingLocationUpdates = true
} else {
//⑧b 制限された機能が無いままで継続
toastMake(R.string.message2)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
//③ リクエストが必要になるまで待機");
//③ リクエストが必要になるまで待機");
button.setOnClickListener {
//④ 権限が既に付与されているか
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
)
== PackageManager.PERMISSION_GRANTED
) {
//⑧a 制限された機能にアクセスする
requestingLocationUpdates = true
} else if (ActivityCompat.shouldShowRequestPermissionRationale(
this,
Manifest.permission.ACCESS_FINE_LOCATION
)
) {
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
//⑤b 権限が必要な理由・メリットを説明
var negativeButton = builder.setMessage(R.string.alert_dialog)
.setPositiveButton(R.string.ok) { _, _ ->
requestPermissionLauncher.launch(
Manifest.permission.ACCESS_FINE_LOCATION
)
}
.setNegativeButton(R.string.no_thanks) { _, _ ->
toastMake(R.string.message1)
}
builder.create()
builder.show()
} else {
//⑥ システム権限を要求する
requestPermissionLauncher.launch(
Manifest.permission.ACCESS_FINE_LOCATION
)
}
}
locationRequest = LocationRequest.create().apply{
interval = 10000
fastestInterval = 5000
// priority = LocationRequest.PRIORITY_HIGH_ACCURACY
priority = Priority.PRIORITY_HIGH_ACCURACY
}
locationCallback = object : LocationCallback() {
override
fun onLocationResult(locationResult: LocationResult) {
for (location in locationResult.locations) {
val textView1 = findViewById<TextView>(R.id.text_view1)
val textView2 = findViewById<TextView>(R.id.text_view2)
// 緯度の表示
val str1 = " Latitude:" + location.latitude
textView1.text = str1
// 経度の表示
val str2 = " Longitude:" + location.longitude
textView2.text = str2
// Google Map
moveToGMap(location)
}
}
}
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
val button2: Button = findViewById(R.id.button2)
button2.setOnClickListener {
// アプリのSetting画面を開く
val uriString = "package:$packageName"
val intent = Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse(uriString)
)
startActivity(intent)
}
}
private fun toastMake(str: Int) {
val toast = Toast.makeText(this, str, Toast.LENGTH_SHORT)
toast.show()
}
private fun startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION
)
!= PackageManager.PERMISSION_GRANTED
) {
return
}
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
}
@SuppressLint("QueryPermissionsNeeded")
private fun moveToGMap(location: Location) {
val str1 = java.lang.String.valueOf(location.latitude)
val str2 = java.lang.String.valueOf(location.longitude)
// geo:[lat,lng][?param[¶m]...], param:z=zoom
val uri: Uri = Uri.parse("geo:$str1,$str2?z=14")
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
private fun stopLocationUpdates() {
fusedLocationClient.removeLocationUpdates(locationCallback)
}
override fun onResume() {
super.onResume()
if (requestingLocationUpdates) {
startLocationUpdates()
}
}
override fun onPause() {
super.onPause()
stopLocationUpdates()
}
}