如何使用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'
}
答案 0 :(得分:3)
没有可靠的方法从SIM卡获取电话号码。 TelephonyManager从SIM卡读取电话号码,但电信运营商可以在SIM卡中添加此信息。
大多数电信运营商都没有在SIM卡中添加此信息,因此不够可靠。
在$mapcontent = json_decode($content,true);
$resultCount = count($mapcontent['results']);
if( $resultCount > 0 ) {
$lat = $mapcontent['results'][0]['geometry']['location']['lat'];
$lng = $mapcontent['results'][0]['geometry']['location']['lng'];
}
中添加以下依赖项:
build.gradle
在dependencies {
...
compile 'com.google.android.gms:play-services:11.6.0'
compile 'com.google.android.gms:play-services-auth:11.6.0'
}
中创建两个常量:
MainActivity.java
在按钮的 private static final int PHONE_NUMBER_HINT = 100;
private final int PERMISSION_REQ_CODE = 200;
中添加以下内容:
onclick
添加onActivityResult
final HintRequest hintRequest =
new HintRequest.Builder().setPhoneNumberIdentifierSupported(true).build();
try {
final GoogleApiClient googleApiClient =
new GoogleApiClient.Builder(MainActivity.this).addApi(Auth.CREDENTIALS_API).build();
final PendingIntent pendingIntent =
Auth.CredentialsApi.getHintPickerIntent(googleApiClient, hintRequest);
startIntentSenderForResult(pendingIntent.getIntentSender(), PHONE_NUMBER_HINT, null, 0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
答案 1 :(得分:1)
您可以使用以下几种方法:
mTelephonyMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);;
String countryiso=mTelephonyMgr.getSimCountryIso();
String simOperator=mTelephonyMgr.getSimOperator();
String simOperatorName=mTelephonyMgr.getSimOperatorName();
String simSerialNo=mTelephonyMgr.getSimSerialNumber();
int simState=mTelephonyMgr.getSimState();
String subscriberID=mTelephonyMgr.getSubscriberId();
<强> 修改 强>
String mPhoneNumber = mTelephonyMgr.getLine1Number();
了解更多信息,visit here.
此许可也是必要的。
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
我希望这会有所帮助。