问题
I am developing an app for Android and want to read the whatsapp telephone number configured in the client device.
Tried the following code:
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
for (Account ac : accounts) {
String acname = ac.name;
String actype = ac.type;
// Take your time to look at all available accounts
System.out.println("Accounts : " + acname + ", " + actype);
}
if(actype.equals("com.whatsapp")){
String phoneNumber = ac.name;
}
I also set this permission: <uses-permission android:name="android.permission.GET_ACCOUNTS" />
but phoneNumber only returns "Whatsapp"
I need the user's whatsapp phone number as a record in my database. How can I get this phone number?
回答1:
Follow these steps:
- You need the
rawcontact
id of WhatsApp of that contact. - Query
ContactsContract.Data
table withmimetype = "vnd.android.cursor.item/vnd.com.whatsapp.profile"
with thatrawcontact
id. - The
DATA3
column is the number of WhatsApp.
回答2:
WhatsApp have updated their process, and they no longer create the account using the phone number as the account name, which is the trick this code relied upon.
I do not believe there is any way to now get hold of the WhatsApp phone number from outside the WhatsApp app.
来源:https://stackoverflow.com/questions/22639640/how-can-i-get-my-whatsapp-number