ki-labs / gps-permission-checks-livedata Goto Github PK
View Code? Open in Web Editor NEWSample project to demonstrate how GPS and Runtime Location Permission checks can be done on UI and Background Service using LiveData
License: MIT License
Sample project to demonstrate how GPS and Runtime Location Permission checks can be done on UI and Background Service using LiveData
License: MIT License
Right after handleGps and handlePermission
Because can't use when() on pair response
Referring to tracking and service flags
https://github.com/Fondesa/KPermissions
I am already using this https://developer.android.com/training/location/change-location-settings or this https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient which also includes using Actvity.onActivityResult to show dialog
call gps instead
I consider myself a beginner regarding Kotlin, so hopefully it is just me that is doing something wrong, but I have been struggling for several days to try to get this LiveData to actually work, but without success.
In the end, I built this app and just added some extra logging in the listener class to see when it actually reacts to permission changes:
class PermissionStatusListener(private val context: Context,
private val permissionToListen: String) : LiveData<PermissionStatus>() {
private val TAG = "* Permission listener "
override fun onActive() = handlePermissionCheck()
private fun handlePermissionCheck() {
val isPermissionGranted = ActivityCompat.checkSelfPermission(context,
permissionToListen) == PackageManager.PERMISSION_GRANTED
Log.d(TAG, " permission status ${isPermissionGranted}")
if (isPermissionGranted)
postValue(PermissionStatus.Granted())
else
postValue(PermissionStatus.Denied())
}
}
sealed class PermissionStatus {
data class Granted(
val message: Int = R.string.permission_status_granted
) : PermissionStatus()
data class Denied(
val message: Int = R.string.permission_status_denied
) : PermissionStatus()
data class Blocked(
val message: Int = R.string.permission_status_blocked
) : PermissionStatus()
}
When the app starts, it shows the correct status of the permission:
2019-12-21 21:55:04.668 11799-11799/com.wahibhaq.locationservicelivedata D/* Permission listener: permission status false
But when the permission is granted, there is no update of the LiveData, only the normal permission handling callbacks:
2019-12-21 22:20:47.899 11799-11799/com.wahibhaq.locationservicelivedata D/Permissions: No rationale.
2019-12-21 22:20:47.939 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: setView = DecorView@b80bcc5[PermissionsActivity] TM=true MM=false
2019-12-21 22:20:47.956 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: dispatchAttachedToWindow
2019-12-21 22:20:47.969 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: Relayout returned: old=[0,63][1080,2094] new=[540,1078][540,1078] result=0x7 surface={valid=true 532950945792} changed=true
2019-12-21 22:20:47.976 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: MSG_RESIZED: frame=Rect(540, 1078 - 540, 1078) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
2019-12-21 22:20:51.313 11799-11799/com.wahibhaq.locationservicelivedata D/Permissions: Just allowed.
2019-12-21 22:20:51.363 11799-11799/com.wahibhaq.locationservicelivedata I/MainActivity$permissionHandler: Activity: 2131427403
2019-12-21 22:20:51.381 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
2019-12-21 22:20:51.381 11799-11799/com.wahibhaq.locationservicelivedata D/InputMethodManager: prepareNavigationBarInfo() DecorView@b80bcc5[PermissionsActivity]
2019-12-21 22:20:51.413 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: MSG_WINDOW_FOCUS_CHANGED 0 0
2019-12-21 22:20:51.428 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: setWindowStopped(true) old=false
2019-12-21 22:20:51.433 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: dispatchDetachedFromWindow
2019-12-21 22:20:51.434 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: Surface release. android.view.ViewRootImpl.doDie:7979 android.view.ViewRootImpl.die:7947 android.view.WindowManagerGlobal.removeViewLocked:497 android.view.WindowManagerGlobal.removeView:435 android.view.WindowManagerImpl.removeViewImmediate:124 android.app.ActivityThread.handleDestroyActivity:4753 android.app.servertransaction.DestroyActivityItem.execute:39 android.app.servertransaction.TransactionExecutor.executeLifecycleState:145
2019-12-21 22:20:51.644 11799-11799/com.wahibhaq.locationservicelivedata D/ViewRootImpl@ac9ab27[PermissionsActivity]: Surface release. android.view.ViewRootImpl$ViewRootHandler.handleMessage:4825 android.os.Handler.dispatchMessage:106
To me it seems that the LiveData is only updated when the observing activity is resumed, and not directly when the permission is granted.
Am I missing something obvious, or does the Android framework not allow this listener to see the changed permission and update the LiveData immediately?
Hello!
The application does not work pointing to the api 29 and changing the libraries to androidx. Could you update the code please?
Greetings,
Patrick
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.