mardi 30 juin 2020

Create `SensorEvent` object using reflecion in Emulator not working

I want to create a SensorEvent object to simulate Sensor data. So i have used this reflection code to create SensorEvent

private fun getSensorEventObject(size : Int) : SensorEvent?{
    val cons = SensorEvent::class.constructors.toList()
    if (cons.isNotEmpty()) {
        return cons[0].call(size)
    }
    return null
}

This is working fine in real device. But when i run this code in Emulator this doesn't work. I got this exception

kotlin.reflect.full.IllegalCallableAccessException: java.lang.IllegalAccessException: Class java.lang.Class<kotlin.reflect.jvm.internal.calls.CallerImpl$Constructor> cannot access  method void android.hardware.SensorEvent.<init>(int) of class java.lang.Class<android.hardware.SensorEvent>

Not sure, as Emulator don't have real hardware is this the reason i am getting exception.

Is there any way i can create SensorEvent object when app is running in Emulator ?





Aucun commentaire:

Enregistrer un commentaire