1

I'm trying to get the phone number using below code,

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phoneImei = tm.getDeviceId();
phoneNo = tm.getLine1Number();

but, it returns me null value. When I checked the setting, phone number was unknown. I'm using galaxy nexus. Any idea how i can solve this.?

chinna_82
  • 6,353
  • 17
  • 79
  • 134

5 Answers5

2

Here is my code :

 import java.lang.reflect.Method;

 import android.app.NotificationManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.media.AudioManager;
 import android.os.Bundle;
 import android.telephony.TelephonyManager;

 import com.android.internal.telephony.ITelephony;

  public class BlockBlackListIncomingNumber extends BroadcastReceiver {
 private Context cont;
 private Transactions transactions;
 private ITelephony itelephony;;

 int i = 1;


    /* When any incoming calls come to our cellphone number */
@Override
public void onReceive(Context context, Intent intent)
            {
    transactions = new Transactions(context); 
                // Create an object of transaction class.
    this.cont = context; 
                 // Refer to current object of Context.
    Bundle extras = intent.getExtras(); 
                 // Getting Bundles in extras.
    String iNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
                 // Getting Incoming Number by which call comes that’s why we used TelephoyManager.EXTRA_INCOMING_NUMBER. 

iNumber will be incoming number.

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
1

You need to add following permission in your AndroidManifest.xml file

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • do you have SIM in your device ? – Lucifer Sep 20 '12 at 10:47
  • do you have GSM SIM because [getLine1Number()](http://developer.android.com/reference/android/telephony/TelephonyManager.html#getLine1Number()) Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. – Lucifer Sep 20 '12 at 10:58
0

Try implementing ITelephony interface using AIDL

yogeshhkumarr
  • 306
  • 1
  • 16
0

In some countries you will not get the telephone number using your code

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); phoneImei = tm.getDeviceId(); phoneNo = tm.getLine1Number();

You will have to get it input from the user. However to verify that number, you will need to include a send SMS and Receive SMS permission in the manifest file.

Using the sender and receiver's number and the crypted message(which you composed/coded) for verification, you can verify the user's telephone number. Whatsapp does it this way.

Vaibhav Ranglani
  • 215
  • 1
  • 2
  • 14
0

If you are not getting line number you can use this code

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    List<SubscriptionInfo> _sb = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
                    for (int i = 1; i < _sb.size(); i++) {
                        SubscriptionInfo info = _sb.get(i);
                        Log.d("phone", "Mobile_number " + info.getNumber());

                    }
                }