Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6564677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:59:49+00:00 2026-05-25T13:59:49+00:00

I have the following code. int phoneContactID = new Random().nextInt(); Cursor contactLookupCursor = context.getContentResolver().query(

  • 0

I have the following code.

        int phoneContactID = new Random().nextInt();

    Cursor contactLookupCursor =  context.getContentResolver().query( Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(contactNumber)), new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},null,null,null);     

    try 
    {
        contactLookupCursor.moveToFirst();
        while(contactLookupCursor.moveToNext())
        {
            phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
         }
    } 
    finally 
    {
        contactLookupCursor.close();
    }       

The problem in the above code is that even if I give a existing number in emulator contacts, its not returning any results. I was testing it an hour back and it was working fine, and now when I tested it again, its not returning anything. I am not sure if anything is wrong with the code. What I am trying to do is get a ID that matches a single with multiple numbers. For instance say there is a contact name called “A” and A has two numbers. Essentially the contact ID for A should be 1 regardless of which number I refer to. Is my assumption correct ?

UPDATE : I did some more tests. Let’s say if a number is stored without the country code in the contacts database like 222-222-2222. A search using the below code returns contact id only when I pass 2222222222 or 222-222-2222. And if the same number is stored like 12222222222 a valid contact id is received only if I search number is 12222222222.

        String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
    Uri contactUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(contactNumber));
    Cursor c = context.getContentResolver().query(contactUri, projection, null, null, null);
    if (c.moveToFirst()) {
        phoneContactID = c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
    }

I am not sure if I am doing something wrong here. Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:59:50+00:00Added an answer on May 25, 2026 at 1:59 pm
    public static int getContactIDFromNumber(String contactNumber,Context context)
    {
        contactNumber = Uri.encode(contactNumber);
        int phoneContactID = new Random().nextInt();
        Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,contactNumber),new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
            while(contactLookupCursor.moveToNext()){
                phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
                }
            contactLookupCursor.close();
    
        return phoneContactID;
    }
    

    The code that is working now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following code: MyType<int> anInstance = new MyType<int>(); Type type =
I have the following code in my controller: public ActionResult Details(int id) { var
I have the following fields: Inventory control (16 byte record) Product ID code (int
I have seen the following code: [DefaultValue(100)] [Description(Some descriptive field here)] public int MyProperty{...}
Suppose I have the following C code. unsigned int u = 1234; int i
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
I have following code int wordLenght = 256, arrayLength = 2, i = 0,
I have the following code : int *edges[500]; char arr[] = {'c','d'}; edges[0] =
I have the following code: int rc; rc = sqlite3_exec(sqlite3_database, .import mydata.csv mytable, callback,
I have the following code: int takeEven(int *nums, int numelements, int *newlist) { newlist

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.