How To Fetch Owner Mobile Number From Sms Inbox/sent
Solution 1:
You can try the following ways to get the mobile number:
TelephonyManager - Will not work for most of the numbers TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); final String contactNumber = telephonyManager.getLine1Number();
AccountManager - Fetch number from accounts like WhatsApp and Telegram AccountManager accountManager = AccountManager.get(context); Account[] accounts = accountManager.getAccounts(); String contactNumber = null; if (accounts.length > 0) { for (Account acc : accounts) { if ("com.whatsapp".equals(acc.type)) { if (!acc.name.matches(".[a-zA-Z]+.")) { contactNumber = acc.name; } } if (contactNumber == null && "org.telegram.messenger.account".equals(acc.type)) { if (!acc.name.matches(".[a-zA-Z]+.")) { contactNumber = acc.name; } } } }
Integrate with Sms API like https://www.twilio.com/ Ask user for number and send sms to the user's number with some authentication code. Either ask user to input the authentication code or the app can directly read the incoming message, parse the authentication code and validate. It might not work with full DND registered numbers from India.
- Missed Call!!! Integrate with service like http://dial2verify.in/ Ask user for phone number, make a call to dial2verify, it will return a phone number, ask user to give a missed call to the number. Once the missed call is received, dial2verify will make a call to your server with validate number details.
Solution 2:
content://
has not worked well for me in the past. It seems that this changes based on the phone that the person is using.
I personally use this to get the phone number of the user:
TelephonyManagertMgr= (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
ownerPhoneNumber = tMgr.getLine1Number();
If you NEED to do it that way though, I use:
Uriuri= Uri.parse("content://sms/");
Cursorcursor= contentResolver.query(uri, null, null, null, null);
cursor.moveToFirst();
Stringtype= cursor.getString(cursor.getColumnIndex("type"));
if (type.equals("2"))
{
//An outgoing SMS was sent.
}
You can grab the phone number using cursor.getString(cursor.getColumnIndex(""))
(Although I forgot what column goes inside there for phone number. You can try a loop like you did above to see what values you receive.
Post a Comment for "How To Fetch Owner Mobile Number From Sms Inbox/sent"