Test Script

Thursday, May 9, 2013

Fetching User's Mobile Number - Android


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;
   }

}



================================================================

You also need a permission to access the Accounts


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


================================================================

As always feel  free to comment or fire any queries to me ! 

7 comments:

  1. getLine2Number() is not available in android api. And also getLine1Number() not working. How to solve this problem.

    ReplyDelete
    Replies
    1. You need to try this on a real phone , it wont work on a emulator. Could you tell me what error are you getting?

      Delete
    2. it returns null value

      Delete
    3. I have same problem, It returns null value..

      Delete
    4. Ooh, let me quickly check that today and get back to you guys

      Delete
    5. Facing same issue :( When are you going to revert back?

      Delete
    6. I have updated the post, I am sorry if you didn't see it.

      anyways 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

      Delete

UA-42774700-1 Twitter Bird Gadget