2017-07-29 60 views
-1

我正在通過firebase使用Google auth,並正在成功登錄用戶。我也檢索了電話簿(設備)中的聯繫人列表,並在我的應用程序的片段中的列表視圖中顯示它。但是現在我希望向我的聯繫人中顯示的用戶安裝了我的應用,這樣當他們點擊時他們將與他們進行私密聊天,其他聯繫人在點擊時可以發送應用邀請。簡而言之:我想查看在其設備上安裝應用程序的聯繫人列表。Firebase:Android:如何顯示已安裝應用程序的聯繫人列表已安裝

+2

請告訴我們你有什麼嘗試到目前爲止,你面臨什麼問題。 –

+0

您如何在您的聯繫人列表中指出誰安裝了應用程序,誰沒有安裝?也就是說,如果我查看了所有聯繫人,那麼有人會告訴我聯繫人已安裝該應用程序? – Jay

回答

0

無法直接列出聯繫人。您需要爲Firebase數據庫中的用戶創建一個節點,以在註冊後存儲用戶詳細信息,然後您可以檢索這些用戶詳細信息。

0

我讓你意味着你正在使用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權限。 同樣在firebase規則中,「write」鍵值必須爲「true」才能寫入數據庫。

0

首先檢索聯繫人列表..

'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(); 
      } 
     }' 

此之後,你可以保持,可以存儲身份驗證的用戶的信息的火力數據庫表,你可以用你的火力用戶的數據庫中讀取列表同步您的聯繫人。

'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) { 

      } 
     });' 
+0

您會如何建議*檢索Firebase身份驗證用戶列表*?你有沒有提供解決方案的代碼示例? – Jay

+0

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/16867971) –

+0

我敢肯定,這不提供解決方案,因爲一些聯繫人已安裝應用程序,有些則沒有 - 沒有標誌來標示哪一個。它也沒有提供點擊的方式去私人聊天室。 – Jay

0

啓用並實現電話號碼登錄在火力的方法,所以U可以檢索火力接觸和比較它將本地聯繫人列表後,其易於實現UR邏輯

相關問題