【问题标题】:How can I get Gps button status based on flow in the Jetpack Compose如何根据 Jetpack Compose 中的流量获取 Gps 按钮状态
【发布时间】:2022-01-05 15:12:03
【问题描述】:

我想在关闭 gps 时看到设置对话框(需要)来关闭 gps,因为我的应用需要打开 GPS。我已经为此编写了代码并将其放在 MainActivity 中的onCreate() 中,但该对话框仅在应用程序运行时显示,但我想在我关闭应用程序中的 GPS 时看到此对话框。

val settingsClient = LocationServices.getSettingsClient(this)
        val locationRequest = LocationRequest()
        val builder =
            LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
                .setAlwaysShow(false)
                .setNeedBle(false)
        settingsClient.checkLocationSettings(builder.build())
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val response = task.result ?: return@addOnCompleteListener
                    val locationSettingsStates =
                        response.locationSettingsStates
                    Log.e("yyy", locationSettingsStates.toString())
                    // TODO
                }
            }
            .addOnFailureListener { e ->
                Timber.i("checkLocationSetting onFailure:" + e.message)
                when ((e as ApiException).statusCode) {
                    LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
                        Timber.i("Location settings are not satisfied. Attempting to upgrade " + "location settings ")
                        try {
                            // Show the dialog by calling startResolutionForResult(), and check the
                            // result in onActivityResult().
                            val rae = e as ResolvableApiException
                            rae.startResolutionForResult(this, 0)
                        } catch (sie: IntentSender.SendIntentException) {
                            Timber.i("PendingIntent unable to execute request.")
                        }
                    }
                    else -> {
                    }
                }
            }
    }

【问题讨论】:

    标签: android google-maps android-jetpack-compose android-jetpack


    【解决方案1】:

    这是一种方法:

    我创建了一个 LocationUtil 类:

    class LocationUtil(context: Context) {
        companion object {
            const val MIN_TIME: Long = 1000L
            const val MIN_DISTANCE: Float = 0.0f
        }
    
        private val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        private var locationListener: LocationListener? = null
    
        val locationStateFlow = MutableStateFlow<Location>(Location(LocationManager.GPS_PROVIDER))
        val gpsProviderState = mutableStateOf(false)
        val isStart: MutableState<Boolean> = mutableStateOf(false)
    
        private val locHandlerThread = HandlerThread("LocationUtil Thread")
    
        init {
            locHandlerThread.start()
        }
    
        @SuppressLint("MissingPermission")
        fun start(minTimeMs: Long = MIN_TIME_MS, minDistanceM: Float = MIN_DISTANCE_M) {
            locationListener().let {
                locationListener = it
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTimeMs, minDistanceM, it, locHandlerThread.looper)
            }
            gpsProviderState.value = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            isStart.value = true
        }
    
        fun stop() {
            locationListener?.let {
                locationManager.removeUpdates(it)
            }
            isStart.value = false
        }
    
        private fun locationListener() = object : LocationListener {
            override fun onLocationChanged(location: Location) {
                locationStateFlow.value = location
            }
    
            override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
            }
    
            override fun onProviderEnabled(provider: String) {
                gpsProviderState.value = true
            }
    
            override fun onProviderDisabled(provider: String) {
                gpsProviderState.value = false
            }
        }
    
    }
    

    我创建了一个包含一些功能的文件:

    private const val REQUEST_CODE_LOCATION_SOURCE_SETTINGS = 200
    
    fun getLocationManager(context: Context): LocationManager =
        context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
    
    fun isLocEnable(context: Context): Boolean {
        val locationManager = getLocationManager(context)
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
    }
    
    @Composable
    fun IsLocationEnable(context: Context) {
        if (!isLocEnable(context)) {
            SimpleAlertDialog(
                title = stringResource(id = R.string.title),
                text = stringResource(id = R.string.dialog_gps_setting),
                singleButton = false,
                confirmText = stringResource(R.string.settings),
                dismissText = stringResource(R.string.cancel),
                onConfirm = {
                    if (it) {
                        Intent(
                            Settings.ACTION_LOCATION_SOURCE_SETTINGS,
                            Uri.fromParts(
                                "package",
                                context.packageName,
                                null
                            )
                        ).also { intent ->
                            try {
                                context.startActivity(
                                    intent
                                )
                            } catch (e: ActivityNotFoundException) {
                                Intent(
                                    Settings.ACTION_LOCATION_SOURCE_SETTINGS
                                ).also { intentCatch ->
                                    context.startActivity(
                                        intentCatch
                                    )
                                }
                            }
    
                        }
                    }
                })
        }
    }
    
    @Composable
    fun LocationState(context: Activity) {
        IsLocationEnable(context)
    }
    

    最后我在 MainActivity 中使用了代码:

     Box(modifier = Modifier.fillMaxSize()) {
                            Column(modifier = Modifier.fillMaxSize()) {
                                ConnectivityStatus()
                                ClientScaffold(clientNavigator)
                            }
                            val gpsEnable by locationUtil.gpsProviderState
                            if (!gpsEnable) {
                                IsLocationEnable(this@MainActivity)
                            }
                        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2022-11-02
      • 2021-08-03
      • 2021-03-05
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多