Hi guys in this tutorial I am going to be showing you how to fetch the user's mobile number programmatically. This is a very short & simple tutorial so without wasting further time lets get to work :
================================================================
MainActivity.java
================================================================
package com.example.testmobiledemo; import android.os.Bundle; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String number = getMobileNumber(); Toast.makeText(getApplicationContext(), number, 500).show(); } public String getMobileNumber(){ TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); String strMobileNumber = telephonyManager.getLine1Number(); // Note : If the phone is dual sim, get the second number using : // telephonyManager.getLine2Number(); return strMobileNumber; } }
================================================================
So add the above permission to your Manifest file.
================================================================
So, let me quickly explain what i did above :
1) I created a method which fetches the User's Mobile Number
2) We use Telephony Manager Class to get the number of the device
3) "telephonyManager.getLine1Number(); " gives me the Mobile Number of the first Sim.
4) Finally added a permission of READ_PHONE_STATE required to access the TELEPHONY Manager.
Thats all there is, this is used to fetch user's Mobile Number programmatically.
Update : This method may not work for all devices, as not all manufacturers support this and the Android Documentation for this is unofficial. So, if you get a null value its just that your manufacturer isn't supporting it
Update : This method may not work for all devices, as not all manufacturers support this and the Android Documentation for this is unofficial. So, if you get a null value its just that your manufacturer isn't supporting it
================================================================
As always feel free to comment or fire any queries to me !
getLine2Number() is not available in android api. And also getLine1Number() not working. How to solve this problem.
ReplyDeleteYou need to try this on a real phone , it wont work on a emulator. Could you tell me what error are you getting?
Deleteit returns null value
DeleteI have same problem, It returns null value..
DeleteOoh, let me quickly check that today and get back to you guys
DeleteFacing same issue :( When are you going to revert back?
DeleteI have updated the post, I am sorry if you didn't see it.
Deleteanyways here is the response :
Update : This method may not work for all devices, as not all manufacturers support this and the Android Documentation for this is unofficial. So, if you get a null value its just that your manufacturer isn't supporting it