skip to Main Content

In my flutter app, I want to implement one feature, that when the app will be open, it will initially print the mobile number(considering there is single or multiple SIM card in the device) of the Android device in which the app is running.

I am not sure whether it is possible or not. If possible please let me know the details procedure with required code and configuration.

Note that: I have tried using some of the plugins/packages of flutter given below:

https://pub.dev/packages/sms_autofill

https://pub.dev/packages/mobile_number

https://pub.dev/packages/sim_data

But none of these solution worked for me. Please help to have a solution of this issue?

2

Answers


  1. The truth is, it’s not really possible to reliably retrieve the mobile number of an Android device from within a Flutter app. Here’s why:

    1. Privacy restrictions: Modern Android versions have significantly tightened security around accessing sensitive information like phone numbers. This is a deliberate design choice to protect user privacy.

    2. Inconsistent behavior: Even if you use packages that claim to retrieve the phone number, they often rely on workarounds or deprecated APIs. These methods are unreliable and may work on some devices but fail on others.

    3. User permissions: Even if a method exists, it typically requires explicit user permission. Many users are (rightfully) hesitant to grant such sensitive permissions, especially for features that don’t seem essential to the app’s core functionality.

    4. iOS incompatibility: On iOS, there’s no API to programmatically access the device’s phone number, so any solution would be Android-only at best.

    5. Future-proofing issues: As Android continues to evolve, any workarounds that currently exist are likely to be blocked in future updates, breaking your app’s functionality.

    Given these constraints, it’s generally not feasible or advisable to try to automatically retrieve the device’s phone number in a Flutter app. Instead, it’s better to:

    1. Design your app to work without needing the phone number.
    2. If you absolutely need a phone number, ask the user to input it manually.
    3. Consider alternative methods of user identification or verification that don’t rely on phone numbers.

    In summary, while it might seem like a straightforward task, reliably getting the device’s phone number is not really possible in a way that’s consistent, future-proof, and respectful of user privacy. It’s best to explore other options for whatever functionality you’re trying to implement.

    Login or Signup to reply.
  2. Unfortunately, there is no way to do for now. And I think that it would be the same in the future.

    BTW, alternatively, you can choose one of the following two methods if you’d like to acquire the mobile number to make a phone call.

    // Method 1: Navigate to the system dial Activity.
    SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    List<SubscriptionInfo> subList = subscriptionManager.getActiveSubscriptionInfoList(); // supports multiple cards
    Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:+1xxx")); // country code, e.g. +1. xxx: mobile number
    intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subList.get(0).getSimSlotIndex());
    startActivity(intent);
    
    
    // Method 2: Make a phone call directly.
    if (checkSelfPermission("android.permission.CALL_PHONE") != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{"android.permission.CALL_PHONE"}, 0);
    }
    
    String primarySimId = "";
    String secondarySimId = "";
    SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    List<SubscriptionInfo> subList = subscriptionManager.getActiveSubscriptionInfoList();
    int index = -1;
    for (SubscriptionInfo subscriptionInfo : subList) {
        index++;
        if (index == 0) {
            primarySimId = subscriptionInfo.getIccId();
        } else {
            secondarySimId = subscriptionInfo.getIccId();
        }
    }
    
    TelecomManager telecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
    List<PhoneAccountHandle> list = telecomManager.getCallCapablePhoneAccounts();
    PhoneAccountHandle primaryPhoneAccountHandle = null, secondaryPhoneAccountHandle = null;
    for (PhoneAccountHandle phoneAccountHandle : list) {
        if (phoneAccountHandle.getId() != null && phoneAccountHandle.getId().contains(primarySimId)) {
            primaryPhoneAccountHandle = phoneAccountHandle;
        }
        if (phoneAccountHandle.getId() != null && phoneAccountHandle.getId().contains(secondarySimId)) {
            secondaryPhoneAccountHandle = phoneAccountHandle;
        }
    }
    
    // To call from SIM 1
    Uri uri = Uri.fromParts("tel", "+1xxx", "");
    Bundle extras = new Bundle();
    extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, primaryPhoneAccountHandle);
    telecomManager.placeCall(uri, extras);
    
    // To call from SIM 2
    uri = Uri.fromParts("tel", "+1xxx", "");
    extras = new Bundle();
    extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, secondaryPhoneAccountHandle);
    telecomManager.placeCall(uri, extras);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search