如何在Firebase上比较数字

时间:2019-02-23 15:19:32

标签: database firebase firebase-realtime-database phone-number contact-list

嗨,我正在尝试将自己的Firebase上的数字与我的应用程序进行比较。

我想做的是当用户选择一个联系人时,我希望它告诉用户是否安装了“ app”。

因此,基本上,如果该数字存在于我的Firebase中,则可以将其添加到textview上,如果没有,则会弹出一个对话框。我也没有完成对话框,我希望该对话框向选择的号码发送邀请。

public class Main2Activity extends Activity {

    private FirebaseAuth auth;
    private FirebaseDatabase mFire;
    private FirebaseUser user;
    private TextView displayname;
    DatabaseReference reference;


    DatabaseReference databaseReference;

    static final int PICK_CONTACT_REQUEST = 1;  // The request code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        // adding the pick contact function
        Button select = (Button) findViewById(R.id.button1);
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
                pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
                startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request it is that we're responding to
        if (requestCode == PICK_CONTACT_REQUEST) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                // Get the URI that points to the selected contact
                Uri contactUri = data.getData();
                // trenger to kolloner en for navn og en for nummer
                String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,};

                //den her er for nummber
                String[] projection2 = {ContactsContract.CommonDataKinds.Phone.NUMBER,};

                // Perform the query on the contact to get the NUMBER column
                // We don't need a selection or sort order (there's only one result for the given URI)
                // CAUTION: The query() method should be called from a separate thread to avoid blocking
                // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
                // Consider using CursorLoader to perform the query.
                Cursor cursor = getContentResolver()
                        .query(contactUri, projection, null, null, null);
                cursor.moveToFirst();

                //getting number
                Cursor cursor2 = getContentResolver()
                        .query(contactUri, projection2, null, null, null);
                cursor2.moveToFirst();

                // Retrieve the phone number from the NUMBER column
                int column = (int) cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                int column2 = (int) cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                final String name = cursor.getString(column);
                final String number = cursor2.getString(column2);
                TextView textView;
                textView = findViewById(R.id.textView);
                textView.setText(name);

                textView = findViewById(R.id.textView5);
                textView.setText(number);

                final String number1 = textView.getText().toString();


                final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                if (user != null) {
                    {

                        reference = FirebaseDatabase.getInstance().getReference().child(user.getUid());

                        final TextView finalTextView = textView;
                        reference.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {

                                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                                    String phone = ds.getKey();

                                if (dataSnapshot.child(phone).equals("phone"))

                                     finalTextView.getText().toString();

                                else
                                    try

                                    {
                                        AlertDialog.Builder ad = new AlertDialog.Builder(Main2Activity.this);
                                        ad.setCancelable(true);
                                        ad.setTitle("this number does not exist");
                                        ad.setMessage("invite this user");
                                        ad.setPositiveButton("Get from Market", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                Intent i = new Intent(Intent.ACTION_VIEW,
                                                        Uri.parse("http://market.android.com/"));
                                                startActivity(i);
                                                finish();
                                            }
                                        }).show();

                                } catch(Exception e) { /* handle any exceptions */return; }



                                }
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });
                    }
                }
            }
        }
    }
}

0 个答案:

没有答案