카테고리 없음

android+kotlin 리플렉션을 이용하여 토스트 띄우기

n00bh4cker 2020. 5. 6. 11:37
    fun reflectionExample()
    {
        try {
            val c = Class.forName("android.widget.Toast")
            val m = c.getDeclaredMethod("makeText", Context::class.java,CharSequence::class.java,Int::class.java)
            val show = c.getDeclaredMethod("show")
            val toast = m.invoke(c,applicationContext,"Hello From Reflection",1)
            show.invoke(toast)
        }
        catch (e:Exception)
        {
            e.printStackTrace()
        }
        return
    }