Saturday, March 31, 2012

How to Get Own Mobile Number Programmatically

sometime you have multiple SIM cards and you don't know which one Number you are using.Then you may be want to "Get Own Mobile Number" ?
How I am going to write a small android application which will solve our puzzle.

So here we go.. we need to create a Basic Android App having an Activity like this
GetMyPhoneNoActivity.java
  
package com.kns;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class GetMyPhoneNoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
    
        String number =getMyPhoneNO();
        Toast.makeText(getApplicationContext(), "My Phone No is: "
        +number, Toast.LENGTH_SHORT).show();
        Log.v("Debug", number);        
    }
    
    private String getMyPhoneNO(){
     TelephonyManager mTelephonyMgr;  
     mTelephonyMgr = (TelephonyManager) getSystemService
       (Context.TELEPHONY_SERVICE);   

     String yourNumber = mTelephonyMgr.getLine1Number();
  return yourNumber; 
    }     
}

Then we need to add user permission into manifest file
  

so it will be look like this..
AndroidManifest.xml
  


    

 

    
        
            
                
                
            
        

    


Now if will check this code on Emulator device, The output will be..


Note: As i have read, so far some people have conflict about different behavior of  output.
there are reports that some SIMs cause this method to return null.

Because --? I think there are cases where the phone does not know its own number - it somehow depends on the network provider / SIM card.

So, There is no guaranteed solution to this problem because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. This is especially true in some countries which requires physical address verification, with number assignment only happening afterwards. Phone number assignment happens on the network - and can be changed without changing the SIM card or device (e.g. this is how porting is supported).

I'd like to know your suggestions!!

Thanks!

3 comments:

  1. Unfortunately, in my country Argentina, some phone companies do not record the telephone number in the sim :P
    If you know a solution, you help me a lot, thanks for the post and greetings !!!

    ReplyDelete
    Replies
    1. Hi Gabriel Thank you for your response, btw As I mentioned in the post about Result conflict, so I would say Whenever I'll get time I'll do some R&D and get some help for you but now-days I am quite busy with posting iPhone Tutorials also. Still I will give the link or any sample code If I'll find, Please share your mail ID or send me a mail on "ramdhan.cse@gmail.com". Greetings! from INDIA.

      Delete
  2. RDC ...Thanks ... It helped me dude .. keep rocking ..

    ReplyDelete