如何在Kotlin android中获取SIM卡号
在此代码中,我无法获得权限框,而吐司仅显示消息,而不显示任何phome号
class MainActivity : AppCompatActivity() {
private var wantPermission = Manifest.permission.READ_PHONE_STATE
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val number = getMyPhoneNO()
Toast.makeText(applicationContext, "My Phone No is: $number", Toast.LENGTH_SHORT).show()
Log.v("Debug", number)
}
private fun getMyPhoneNO(): String {
val mTelephonyMgr: TelephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
if (checkSelfPermission(this, wantPermission) != PackageManager.PERMISSION_GRANTED) {
return ""
}
return mTelephonyMgr.line1Number
}
}