【发布时间】:2018-10-27 10:11:59
【问题描述】:
如何使用android studio获取电话号码或sim卡信息? (simcard 1 或 2)
我用过这段代码:
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phone_number.setText(tMgr.getLine1Number());
我还在 AndroidManifest 中添加了权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
但我应用的所有小工具都无法获取电话号码或始终生成空值。
这是我使用的 build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
//noinspection GradleCompatible
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-location:15.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//noinspection UseOfBundledGooglePlayServices
implementation 'com.google.android.gms:play-services:12.0.1'
implementation 'com.google.android.gms:play-services-auth:15.0.1'
}
【问题讨论】:
-
我认为您需要在运行时请求此权限,因为根据文档,它的保护级别是危险的(取决于您使用的 android 版本设备运行)
-
它返回null 导致它不可用,并且大多数情况下情况相同,因此,如果您需要根据sim执行操作(除了调用),那么您应该使用
getSimSerialNumber()跨度> -
@x0r 你的意思是如何在运行时创建权限?
-
@Sam。如何使用 getSimSerialNumber()?
-
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String simID = tm.getSimSerialNumber();