如何以编程方式获取运行我的Android应用程序的设备的电话号码?
我想知道Android中的用户手机号码.我使用了这段代码,但我没有得到数字.
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String n = tm.getLine1Number();
Run Code Online (Sandbox Code Playgroud)
允许:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Run Code Online (Sandbox Code Playgroud)
是否必须在android手机中保存号码 settings --> about phone --> status --> myphone number
有什么想法吗?
如何使用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)