I'm trying to use the vibrator service inside a Thread class but when I do so, I have an error which says "Type mismatch: inferred type is String but Context was expected"
here is my code :
class myThread: Thread() {
override fun run() {
var vibration = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
for(i in 1..5) {
vibration.vibrate(100)
Thread.sleep(1000)
}
}
}
It works in my mainActivity class but it doesn't in a Thread. Thank you in advance for any help.
getSystemServiceis defined in Activity class with the signature below.When you use same method name in any other class, you are using
ContextCompathelper class which requires a context and serviceClass.You may change your
MyThreadclass like below.