3

In my application i want get phone number from sim card and show user.
I write below codes, but in emulator show me sim card number , but some devices such as Samsung not show me any sim card number.

Manifest code:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Activity code:

public class FetchSimActivity extends AppCompatActivity {

    TextView fetchSimCardTxt, fetchNumCardTxt;
    TelephonyManager tm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fetch_sim);
        fetchSimCardTxt = findViewById(R.id.fetchSimCardTxt);
        fetchNumCardTxt = findViewById(R.id.fetchNumCardTxt);
        tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        String simID = tm.getSimSerialNumber();
        if (simID != null) {
            fetchSimCardTxt.setText("SIM ID : + " + simID);
        }

        String telNumber = tm.getLine1Number();
        if (telNumber != null) {
            fetchNumCardTxt.setText("SIM NUM : + " +telNumber);
        }
    }
}

How can i get sim card number from all devices in android?

Hock
  • 147
  • 2
  • 2
  • 14
  • 1
    Possible duplicate of [How to get phone number or sim card information use android studio?](https://stackoverflow.com/questions/50386414/how-to-get-phone-number-or-sim-card-information-use-android-studio) – Sagar Jul 10 '18 at 07:08
  • Try this https://stackoverflow.com/questions/14051023/how-to-get-current-sim-card-number-in-android – Ankita Jul 10 '18 at 07:11
  • Possible duplicate of [Programmatically obtain the phone number of the Android phone](https://stackoverflow.com/questions/2480288/programmatically-obtain-the-phone-number-of-the-android-phone) – Khemraj Sharma Jul 10 '18 at 07:20

4 Answers4

5

Add permission in AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

and then use this code

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    String TAG = "PhoneActivityTAG";
    Activity activity = MainActivity.this;
    String wantPermission = Manifest.permission.READ_PHONE_STATE;
    private static final int PERMISSION_REQUEST_CODE = 1;
    ArrayList<String> _mst=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (!checkPermission(wantPermission)) {
            requestPermission(wantPermission);
        } else {

            Log.d(TAG, "Phone number: " + getPhone());
         _mst = getPhone();

            for (String op: _mst) {
               Log.i("Device Information", String.valueOf(op));
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private ArrayList<String> getPhone() {
        TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(activity, wantPermission) != PackageManager.PERMISSION_GRANTED) {
            return null;
        }
        ArrayList<String> _lst =new ArrayList<>();
        _lst.add(String.valueOf(phoneMgr.getCallState()));
        _lst.add("IMEI NUMBER :-"+phoneMgr.getImei());
        _lst.add("MOBILE NUMBER :-"+phoneMgr.getLine1Number());
        _lst.add("SERIAL NUMBER :-"+phoneMgr.getSimSerialNumber());
        _lst.add("SIM OPERATOR NAME :-"+phoneMgr.getSimOperatorName());
        _lst.add("MEI NUMBER :-"+phoneMgr.getMeid());
        _lst.add("SIM STATE :-"+String.valueOf(phoneMgr.getSimState()));
        _lst.add("COUNTRY ISO :-"+phoneMgr.getSimCountryIso());
        return _lst;
    }

    private void requestPermission(String permission){
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)){
            Toast.makeText(activity, "Phone state permission allows us to get phone number. Please allow it for additional functionality.", Toast.LENGTH_LONG).show();
        }
        ActivityCompat.requestPermissions(activity, new String[]{permission},PERMISSION_REQUEST_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "Phone number: " + getPhone());
                } else {
                    Toast.makeText(activity,"Permission Denied. We can't get phone number.", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    private boolean checkPermission(String permission){
        if (Build.VERSION.SDK_INT >= 23) {
            int result = ContextCompat.checkSelfPermission(activity, permission);
            if (result == PackageManager.PERMISSION_GRANTED){
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}
Saeed
  • 3,294
  • 5
  • 35
  • 52
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15
  • if you are facing any problem in implementing the above code please let me know – Vishal Sharma Jul 10 '18 at 11:46
  • 1
    use this code without using ArrayList simple return phoneMgr.getLine1Number(); – Vishal Sharma Jul 10 '18 at 12:40
  • which API level of your Android device – Vishal Sharma Jul 10 '18 at 13:25
  • only use this line of code inside the onCreate method. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { List _sb = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList(); for (int i = 1; i < _sb.size(); i++) { SubscriptionInfo info = _sb.get(i); Log.d(TAG, "Mobile_number " + info.getNumber()); } } – Vishal Sharma Jul 10 '18 at 14:00
  • I implemented the same code if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { List _sb = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList(); for (int i = 1; i < _sb.size(); i++) { SubscriptionInfo info = _sb.get(i); Log.d(TAG, "Mobile_number " + info.getNumber()); } } BUT IS SHOWING PHONE NUMBER AS "" empty string.? – Gyan Swaroop Awasthi Apr 13 '20 at 13:49
0

In onCreate method.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    List<SubscriptionInfo> _sb = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
    for (int i = 1; i < _sb.size(); i++) {
        SubscriptionInfo info = _sb.get(i);
        Log.d(TAG, "Mobile_number " + info.getNumber());

    }
}
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15
0

use this code

public class MainActivity extends AppCompatActivity {
    String TAG = "PhoneActivityTAG";
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             List<SubscriptionInfo> _sb = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
            for (int i = 1; i < _sb.size(); i++) {
                SubscriptionInfo info = _sb.get(i);
                Log.d(TAG, "Mobile_number " + info.getNumber());

            }
        }
    }
}
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15
0

Get Two sim details.

 if (SubscriptionManager.from(this).getActiveSubscriptionInfoCount() > 0) {
                ArrayList<String> _lst = new ArrayList<>();
                for (int i = 0; i < SubscriptionManager.from(this).getActiveSubscriptionInfoList().size(); i++) {
                    _lst.add("\n".concat(String.valueOf(phoneMgr.createForSubscriptionId(i).getCallState())));
                    _lst.add("\nIMEI NUMBER : " + phoneMgr.createForSubscriptionId(i).getImei());
                    _lst.add("\nMOBILE NUMBER : ".concat(phoneMgr.createForSubscriptionId(i).getLine1Number()));
                    _lst.add("\nSERIAL NUMBER : ".concat(phoneMgr.createForSubscriptionId(i).getSimSerialNumber()));
                    _lst.add("\nSIM OPERATOR NAME : " + phoneMgr.createForSubscriptionId(i).getSimOperatorName());
                    _lst.add("\nMEI NUMBER : " + phoneMgr.createForSubscriptionId(i).getMeid());
                    _lst.add("\nSIM STATE : ".concat(String.valueOf(phoneMgr.createForSubscriptionId(i).getSimState())));
                    _lst.add("\nCOUNTRY ISO : " + phoneMgr.createForSubscriptionId(i).getSimCountryIso());
                }
                return _lst;
            }