我通过firebase使用Google身份验证并成功记录用户。我还从电话簿(设备)中检索了联系人列表,并将其显示在我的应用程序中的片段中的列表视图中。但现在我希望向我的联系人中显示安装了我的应用程序的用户,以便点击时他们会与他们进行私聊,其他联系人在点击时会启用他们发送应用邀请。简而言之:我想查看在其设备上安装了应用程序的联系人列表。
无法直接列出联系人。您需要为firebase数据库中的用户创建一个节点,以便在注册后存储用户详细信息,然后您可以检索这些用户详细信息。
我告诉你你正在使用firebase。现在,如果安装在一个设备中,您希望应用程序在firebase数据库中将所有联系人上传到您的服务器。
试试以下代码:
public class YourActivity extends AppCompatActivity {
ProgressDialog dialog;
DatabaseReference yourReference;//your database reference
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
yourReference = FirebaseDatabase.getInstance().getReference().child("users");
setContentView(R.layout.activity_your);
dialog = new ProgressDialog(this);
dialog.setMessage("Uploading contacts...");
// Query for contacts through content resolver. You will get a cursor.
Cursor contacts = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
},
null,
null,
null
);
// If you have a list as your data, firebase facilitates you to upload that easily by a single HashMap object. Create a HashMap object.
HashMap<String,Object> map = new HashMap<>();
// Loop contacts cursor with map to put all contacts in map. I used contact name as key and number as its value (simple and pretty way).
if(contacts!=null) {
while(contacts.moveToNext()){
map.put(
contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),
contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
);
}
contacts.close();
}
dialog.show();
//write map to firebase database reference...
yourReference.updateChildren(map)
//this onSuccessListener is optional. You can terminate above line of code by ";" (semicolon).
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
dialog.dismiss();
Toast.makeText(YourActivity.this, "Contacts uploaded suffessfully!", Toast.LENGTH_SHORT).show();
}
})
//this onFailureListener is also optional.
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dialog.dismiss();
Log.w("MKN","Error: "+e.getMessage());
Toast.makeText(YourActivity.this, "Contacts upload failed.", Toast.LENGTH_SHORT).show();
}
});
}
}
您需要提供READ_CONTACTS权限才能查询Contacts表。同样在firebase规则中,“write”键的值必须为“true”才能写入数据库。
首先检索联系人列表..
'ContentResolver cr = getContext().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
HashMap<String, String> map = new HashMap<>();
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if( email != null ){
map.put("name", name);
map.put("email", email);
getContactList.add(map);
}
}
cur1.close();
}
}'
在此之后,您可以维护一个可以存储经过身份验证的用户信息的firebase数据库表,您可以将联系人与您从firebase用户的数据库中提取的列表同步。
'mapChat = new HashMap<>();
Log.d("Debug", clist.toString());
userReference1 = FirebaseDatabase.getInstance().getReference().child("Users");
userReference1.keepSynced(true);
userReference1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (int x = 0; x < clist.size(); x++) {
//Log.d("Debug" ,list.get(x).get("email").toString());
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
if (dsp.hasChild("email")) {
// Log.d("Debug" , "setnewuser " + dsp.child("email").getValue().toString());
if (dsp.child("email").getValue().toString().equals(clist.get(x).get("email").toString())) {
Log.d("Debug", "contact updated");
String uid = dsp.getKey().toString();
reference1 = FirebaseDatabase.getInstance().getReference().child("Users").child(id).child("contacts").child(uid);
mapChat.put("name", clist.get(x).get("name"));
mapChat.put("email", clist.get(x).get("email"));
mapChat.put("chats", "false");
reference1.setValue(mapChat);
}
}
}
}
reference1.onDisconnect();
contactIdInterface1.contactUpdated();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});'
在firebase中启用并实现电话号码登录方法,这样你就可以从firebase中检索联系人并将其与本地联系人列表进行比较,之后便于实现你的逻辑