alexandre9865
alexandre9865

Reputation: 553

Call function from android library

According to UserManager Documentation, there's some public methods like the getUserName(), when I call the function as below and I compile, I get the error: Unresolved reference: getUserName

package com.example

import android.Manifest
import android.os.UserManager

class DeviceInfo(private val context: Context) {

    val userName: String
        get(){
            return UserManager.getUserName()
        }


}

How can I call this public function in Kotlin?

Upvotes: 0

Views: 148

Answers (1)

DoubleD
DoubleD

Reputation: 148

Try the following code, it should solve your problem

class DeviceInfo(private val context: Context) {
    val userName: String
        get(){
            val um = context.getSystemService(Context.USER_SERVICE) as UserManager
            return um.userName
        }
}

Upvotes: 3

Related Questions