Reputation: 6403
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.?
Upvotes: 1
Views: 5110
Reputation: 4750
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());
}
}
Upvotes: 0
Reputation: 213
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.
Upvotes: 0
Reputation: 4306
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.
Upvotes: 2
Reputation: 29672
You need to add following permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Upvotes: 1