Nakshatran
Nakshatran

Reputation: 426

Unable to get phone number from my android app?

I am trying to get and show the phone number of my mobile from my android app, its working in emulator but not on real device(mobile).

Can any one tell me the reason?

here is source

AndroidMenuActivity.java

public class AndroidMenusActivity extends Activity {
   TextView txt;
   String mphone;

   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   txt = (TextView)findViewById(R.id.txtVw_phonenumber);

   TelephonyManager tmgr = 
             (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

   mphone = tmgr.getLine1Number();

   txt.setText(mphone);

   System.out.println("*******************************"   + mphone);
   }   
}

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.androidhive.androidmenus"
  android:versionCode="1"
  android:versionName="1.0">
  <uses-sdk android:minSdkVersion="8" />


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

  <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".AndroidMenusActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 </application>
</manifest>

Upvotes: 3

Views: 3687

Answers (4)

Mohit Rakhra
Mohit Rakhra

Reputation: 165

In GSM phone we actually cannot do this thing.. however we can find the network information through TelephonyManager it gives a empty string because Phone number is not embeded in the sim_card memory.. You can extract the sim_card serial number and from that you can map the phone number if you have some other resources to achieve this.

You can do the following..

public String getSimSerialNumber ()

Added in API level 1

Requires Permission: READ_PHONE_STATE

and you can also fetch the MCC and MNC code of the provider of the SIM.

If you have the links in telco's you can figure this out..very easily..

Upvotes: 0

Rupali
Rupali

Reputation: 774

TelephonyManager tmgr = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);

Can u check using application context ?

Upvotes: 1

Buda Gavril
Buda Gavril

Reputation: 21647

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 know it is pain, but most likely the best solution is just to ask the user to enter his/her phone number once and store it. See here

Upvotes: 1

Rohit
Rohit

Reputation: 490

Getting phone number is only possible on CDMA device. On GSM device, you will be able to get phone no.

TelephonyManager telephone=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
  String mPhoneNumber = telephone.getLine1Number();

Upvotes: 0

Related Questions