Reputation: 79
In my application, I want to get user's sim number if present, i have tried below code but it returns ""
TelephonyManager tMgr =
(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Required permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
I have searched everywhere to find a method to get user's sim number but nothing worked for me. Some people saying maybe user's sim provider have restriction over this feature but I use imo application that get my sim number and displays in application so I have tried different methods on same mobile but nothing works, if anyone know working method kindly tell me.
Upvotes: 2
Views: 1226
Reputation: 307
Enable permission in Manifest ,get your SMI Number and Imei Number
Manifest.Java
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
MainActivity.Java
public class MainActivity extends AppCompatActivity {
Activity mActivity;
String mStatus;
private TextView mSmiTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = MainActivity.this;
mSmiTxt = (TextView) this.findViewById(R.id.smitxt);
mStatus=loadSMI1();
mSmiTxt.setText(mStatus);
/** Fading Transition Effect */
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
/**
* Called when the 'loadIMEI' function is triggered.
*/
public void loadIMEI() {
// Check if the READ_PHONE_STATE permission is already available.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestReadPhoneStatePermission();
} else {
// READ_PHONE_STATE permission is already been granted.
loadImei();
}
}
public String getDeviceUniqueID() {
String device_unique_id = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ANDROID_ID);
return device_unique_id;
}
private void requestReadPhoneStatePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_PHONE_STATE)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
new AlertDialog.Builder(MainActivity.this)
.setTitle("Permission Request")
.setMessage(getString(R.string.permission_read_phone_state_rationale))
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//re-request
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
})
.setIcon(R.drawable.error)
.show();
} else {
// READ_PHONE_STATE permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
}
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_READ_PHONE_STATE) {
// Received permission result for READ_PHONE_STATE permission.est.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has been granted, proceed with displaying IMEI Number
//alertAlert(getString(R.string.permision_available_read_phone_state));
loadImei();
} else {
alertAlert(getString(R.string.permissions_not_granted_read_phone_state));
}
}
}
public String loadImei() {
//Have an object of TelephonyManager
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//Get IMEI Number of Phone //////////////// for this example i only need the IMEI
String IMEINumber = tm.getDeviceId();
return IMEINumber;
}
private void alertAlert(String msg) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Permission Request")
.setMessage(msg)
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do somthing here
}
})
.setIcon(R.drawable.error)
.show();
}
public String loadSMI1() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String sim1 = tm.getLine1Number();
return sim1;
}
}
If the SMI number returns null ,get it from whatsapp
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
ArrayList<String> googleAccounts = new ArrayList<String>();
for (Account ac : accounts) {
String acname = ac.name;
String actype = ac.type;
// Take your time to look at all available accounts
System.out.println("Accounts : " + acname + ", " + actype);
}
if(actype.equals("com.whatsapp")){
String phoneNumber = ac.name;
}
Upvotes: 1
Reputation: 1570
Step 1: Add the Required Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
Note: For Devices with 6.0 (marshmallow) and above Use Run Rime Permissions
Step 2:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String sim1= telephonyManager.getLine1Number();
String sim2= telephonyManager.getLine2Number();
Check this for More info
Upvotes: 1
Reputation: 13555
First of all i suggest that you should use shared preference,you just store it and use it in your app wherever you want. it is the easiest way i guess.
OR
try to check in
Phone--> Settings --> About -->
Phone Identity, If you can to view Number there, then may be you can get the number. but if you can not view the phone number in the settings, then you won't be able to get via this code.
Upvotes: 1