Kotlin-设备锁定时如何显示LocationSettingsRequest对话框

时间:2019-03-25 15:21:09

标签: android kotlin lockscreen

我有一个活动,该活动由一个事件触发,并且使用此代码,以便能够在我的设备锁定时显示该活动。

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true)
            setTurnScreenOn(true)
            val pm=getSystemService(Context.POWER_SERVICE)as PowerManager
            pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"FireApp:test2").acquire(1000000)
            val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
            keyguardManager.requestDismissKeyguard(this, null)
        }else{

            this.window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON or
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
        }

在我的设备被锁定之前,它可以正常工作,直到我想使用以下代码显示LocationSettingsRequest对话框:

fun displayLocationSettingsRequest(context: Context) {
        val googleApiClient = GoogleApiClient.Builder(context)
                .addApi(LocationServices.API).build()
        googleApiClient.connect()

        val locationRequest = LocationRequest.create()
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
        locationRequest.setInterval(5000)
        locationRequest.setFastestInterval(2000)

        val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
        builder.setAlwaysShow(true)


        val result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build())
        result.setResultCallback (object: ResultCallback<LocationSettingsResult> {
            override fun onResult(result: LocationSettingsResult) {
                val status = result.status
                if (status.statusCode == LocationSettingsStatusCodes.SUCCESS) {
                    Log.d("Martin", "Lokacija je vključena!")
                }
                if (status.statusCode == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                    try {
                        status.startResolutionForResult(this@medAlarmom2, 0x1)
                    } catch (e: IntentSender.SendIntentException) {
                        Log.d("Martin", "PendingIntent unable to execute request.")
                    }

                }
                if (status.statusCode == LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE) Log.d("Martin", "Location settings are inadequate, and cannot be fixed here. Dialog "+"not created.")
            }
        })
    }

我找不到一个好的解决方案,能够在不解锁设备的情况下显示此对话框。有人对此有解决方案吗?

谢谢你! 马丁

0 个答案:

没有答案