从Whatsapp addess获取联系人姓名

时间:2016-11-14 12:56:30

标签: android android-contacts whatsapp

我有来自Whatsapp的联系信息:491766465xxxx@s.whatsapp.net

从此代码中获取

void startWhatsAppContactPicker() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setPackage("com.whatsapp");
        try {
            startActivityForResult(intent, REQUEST_CODE_PICK_WHATSAPP);
        } catch (Exception e) {
            Toast.makeText(this, "Kein Whatsapp installiert", Toast.LENGTH_SHORT).show();
        }
    }


  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_PICK_WHATSAPP:
                if (resultCode == RESULT_OK) {
                    if (data.hasExtra("contact")) {
                        String address = data.getStringExtra("contact");
                        Log.d(TAG, "The selected Whatsapp address is: " + address);
                  }
                }
                break;
            default:
                break;
        }
    }

输出:49176646xxxx@s.whatsapp.net

这些信息是否存储在Android联系人中?

我需要联系人才能获得所有者的姓名。

我试过这个但没有成功:

How to get whatsapp Contacts from Android?

干杯

1 个答案:

答案 0 :(得分:0)

假设49176646xxxx段是电话号码,您可以在电话联系人中搜索该电话号码:

String address = data.getStringExtra("contact");
String phone = address.split("@")[0];
String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER, Phone.NORMALIZED_NUMBER };
String selection = Phone.NORMALIZED_NUMBER + " = " + phone;
Cursor c = cr.query(Phone.CONTENT_URI, projection, selection, null, null);
if (c != null && c.moveToFirst()) {
   Log.d(TAG, "name is: " + c.getString(0));
}

(注意:{16}中添加了Phone.NORMALIZED_NUMBER