Huzaifaアシフ:
私は私の電話番号を取得するための2つの方法を使用して試してみましたが、それらの両方が動作しません。私が使用しました:
- TelephonyManager
- SubscriptionManager
私は、ネットワーク名、国のISO、およびIMEIを得るのですが、私は番号を返すようにしようとするたびに、それは何も返しません。
私はまた、これらのために必要なすべての権限を追加しました!私のマニフェストルックスが好き:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
使用してコードTelephonyManagerを:
TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phoneMgr.getLine1Number()
使用してコードSubscriptionManagerを:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<SubscriptionInfo> subscription = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
for (int i = 0; i < subscription.size(); i++) {
SubscriptionInfo info = subscription.get(i);
Log.e("TAG", "number " + info.getNumber());
Log.e("TAG", "network name : " + info.getCarrierName());
Log.e("TAG", "country iso " + info.getCountryIso());
}
}
両方の試みで、私は何も得ます!
電話番号を取得する他の方法はありますか私は何かを間違ってやっていますか?
Bhoomikaパテル:
今日でTelephonyManager
私たちを助けていません。許可なしにサービスAPIを再生すると、このための良い解決策です。
この依存関係は、この場合に便利です
implementation 'com.google.android.gms:play-services-auth:16.0.1'
今すぐあなたの内側Activity.java:
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Auth.CREDENTIALS_API)
.build();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
この後の要求を行う電話番号を:
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(mGoogleApiClient, hintRequest);
try {
startIntentSenderForResult(intent.getIntentSender(), 1008, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
Log.e("", "Could not start hint picker Intent", e);
}
今、あなたはあなたの中に応答を処理する必要がありonActivityResult
、このように:
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1008:
if (resultCode == RESULT_OK) {
Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
// cred.getId====: ====+919*******
Log.e("cred.getId", cred.getId());
userMob = cred.getId();
} else {
// Sim Card not found!
Log.e("cred.getId", "1008 else");
return;
}
break;
}
}