To check if you currently have a permission you use ContextCompat.checkSelfPermission()
, see the developer docs for details.
If you need the permission in a background thread then you check if you have the permission before you do the work. If you do not have the permission you will have to skip the work, if you just continue then your code will most likely throw a SecurityException
.
How you handle it when you don't have the required permissions depends on the use case. You could create a system notification that informs the user the background task could not be performed due to a missing permission. If the user taps the notification then you can request the permission from the user before retrying the task. See the same developer docs for how to request a permission from the user.
If it would be okay to abort the background task then you could do just that but you will probably want to show somewhere in your user interface that the background task needs a permission that is missing and ask the user to grant it so the (optional) background task can do its work.