I am trying to get phone no using following method:
private String getMyPhoneNumber() {
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//String imsi = mTelephonyMgr.getSubscriberId();
String phnNo = mTelephonyMgr.getLine1Number();
if (phnNo == null) {
phnNo = getNoFromWatsApp();
}
return phnNo;
}
private String getNoFromWatsApp(){
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
String phoneNumber = "";
for (Account ac : accounts) {
String acname = ac.name;
String actype = ac.type;
// Take your time to look at all available accounts
if(actype.equals("com.whatsapp")) {
phoneNumber = ac.name;
}
}
return phoneNumber;
}
But every time I get an empty phone number. I even tried to get phone number from WhatsApp but it's returning "WhatsApp" instead of phone number. Is there any other way to solve this problem?