小编dya*_* tb的帖子

如何使用Android Studio获取电话号码或SIM卡信息?

如何使用Android Studio获取电话号码或SIM卡信息?(SIM卡1或2)

我使用了以下代码:

TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phone_number.setText(tMgr.getLine1Number());
Run Code Online (Sandbox Code Playgroud)

而且我还在AndroidManifest中添加了权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Run Code Online (Sandbox Code Playgroud)

但是我应用的所有小工具都无法获取电话号码或始终生成空值。

这是我使用的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'
}
Run Code Online (Sandbox Code Playgroud)

android telephonymanager

2
推荐指数
1
解决办法
4112
查看次数

标签 统计

android ×1

telephonymanager ×1