Actually I have found the solution to the above problem, well kind of, it
turns out the problem is elsewhere,

I am trying to find the Id of a contact via the phone number and try as
follows but I get an ID of 2324 when it should be 404, anyone any ideas why?

 static {
         int sdk=new Integer(Build.VERSION.SDK).intValue();

         if (sdk>=5) {
         try {
         Class
clazz=Class.forName("android.provider.ContactsContract$CommonDataKinds$Phone");

         CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
         NAME_URI=(String) clazz.getField("DISPLAY_NAME").get(clazz);
         ID_URI=(String) clazz.getField("_ID").get(clazz);

         NUMBER_URI=(String)clazz.getField("NUMBER").get(clazz);

         }
         catch (Throwable t) {
         Log.e("reflection", "Exception when determining CONTENT_URI", t);
         }
         }
         else {
         CONTENT_URI=Contacts.People.CONTENT_URI;

         NAME_URI=People.NAME;
         ID_URI=People._ID;

         NUMBER_URI=People.NUMBER;

         }
         }


----

    public int getContactId(String phoneNumber){

         String [] requestedColumns = {
                 NAME_URI,
                 ID_URI
         };

         Cursor contacts = context.getContentResolver().query(
                 CONTENT_URI,
                 requestedColumns,
                 NUMBER_URI + "='" + phoneNumber + "'",
                 null, null);


         Log.d("GETDETAILS", "Count = " + contacts.getCount());
         if(contacts.getCount() > 0){
         int nameIdx = contacts.getColumnIndex(NAME_URI);
         int idIdx = contacts.getColumnIndex(ID_URI);

         contacts.moveToFirst();
         Log.d("DETAILS", "Name : " + contacts.getString(nameIdx));
         Log.d("DETAILS", "Type : " + contacts.getString(idIdx));
         return contacts.getInt(idIdx);
         }
         else

         return 0;

     }

On Wed, Jul 28, 2010 at 10:07 AM, Donal Rafferty <[email protected]> wrote:

> Thanks Kostya,
>
> I have grasped that now and have it working in one part of my application.
>
> I have another problem now though.
>
> I have this in code:
>
> Uri contactUri = ContentUris.withAppendedId
> (Contacts.People.CONTENT_URI, contactId);
>
>
> Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
> startActivity(intent);
>
> This launchs the contacts application and displays the contact according to
> the ID supplied, this works in all Android versions except 2.2 (API level 8)
> and I cant find the equivalant code for 2.2.
>
> I have tried ContactsContract.PhoneLookup, ContactsContract.Contact,
> ContactsContract.CommonDataKinds but none of them seem to let me launch the
> contacts application and dispaly a contacts info according to a supplied ID.
>
> Can anyone help with this?
>
> On Tue, Jul 27, 2010 at 4:27 PM, Kostya Vasilyev <[email protected]>wrote:
>
>> Hi,
>>
>> The offending line is this:
>>
>> ContactsContract.Contacts
>>
>> Even though you're not referencing this nested member on the execution
>> path, the Java VM still tries to resolve all external references made by
>> your code. This is done when your code is first loaded into memory for
>> execution, before it is run. At this point, the external reference fails
>> (since you are on Android 1.5), and your code is not able to load and
>> execute.
>>
>> The solution is to use Java Reflection feature to not make any
>> compile-time references to missing classes / members, but rather check at
>> runtime whether they exist. This way, there are no compile-time references
>> to stuff that only exists in later API levels, and your code can gracefully
>> fall back to earlier APIs at runtime.
>>
>> The key is what references your code makes at compile time, and, as a
>> consequence, when your class is loaded into memory.
>>
>> http://java.sun.com/developer/technicalArticles/ALT/Reflection/
>>
>> That is the general solution.
>>
>> However, in this particular case, you could duplicate the URI string from
>>
>> ContactsContract.Contacts.CONTENT_URI
>>
>> to some string constant in your code. Just like this:
>>
>> class YourClass {
>>
>> private static final String ANDROID_1_X_CONTACTS_URI = People.CONTENT_URI;
>>
>> private static final String ANDROID_2_X_CONTACTS_URI = "content://<put
>> correct value here>";
>> }
>>
>> Then use the appropriate constant (one of the two) based on API level just
>> like you're trying to do now.
>>
>> -- Kostya
>>
>> 27.07.2010 18:10, [email protected] пишет:
>>
>>  I have the following piece of code in my Android application that
>>> looks up a contacts ID, I have just changed the target from 1.5 to 2.2
>>> but with a min SDK of 3 (1.5) so that I can use the ContactsContract
>>> Uri in 2.2 and the People Uri in 1.5.
>>>
>>> However the code leads to the following error when run on 1.5 devices:
>>>
>>>     07-27 15:02:53.382: WARN/dalvikvm(12656): VFY: unable to resolve
>>> static field 25 (CONTENT_URI) in Landroid/provider/ContactsContract
>>> $Contacts;
>>>
>>> ---
>>>
>>> > From google I have garnered that I need to use reflection in this case
>>> to allow the application run on both versions of Android?
>>>
>>> I have seen example's of how to use reflection to use methods of
>>> multiple/different versions but how can I use it in mycase where I
>>> want to use the ContactsContract Uri?
>>>
>>> Here is my code:
>>>
>>> ---
>>>
>>>
>>>     findViewById(R.id.contactimage).setOnClickListener(new
>>> OnClickListener() {
>>>                            public void onClick(View v) {
>>>
>>>                                String sdk = android.os.Build.VERSION.SDK;
>>>                                Uri contactUri;
>>>                                Log.d("CDA", "Contact ID Button pressed =
>>> " +
>>> contactId);
>>>                                if(sdk.equals("8")){
>>>                                        contactUri =
>>> ContentUris.withAppendedId
>>>
>>>  (ContactsContract.Contacts.CONTENT_URI, contactId);
>>>
>>>                                        Intent intent = new
>>> Intent(Intent.ACTION_VIEW,
>>> contactUri);
>>>                                        startActivity(intent);
>>>
>>>                                }
>>>                                else{
>>>                                contactUri = ContentUris.withAppendedId
>>>                                (People.CONTENT_URI, contactId);
>>>
>>>                                Intent intent = new
>>> Intent(Intent.ACTION_VIEW,
>>> contactUri);
>>>                                startActivity(intent);
>>>                                }
>>>
>>>                                dispatchKeyEvent(new
>>> KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.FLAG_SOFT_KEYBOARD));
>>>                                dispatchKeyEvent(new
>>> KeyEvent(KeyEvent.ACTION_UP,
>>> KeyEvent.KEYCODE_BACK));
>>>                            }
>>>                    });
>>>
>>>
>>>
>>
>>
>> --
>> Kostya Vasilev -- WiFi Manager + pretty widget --
>> http://kmansoft.wordpress.com
>>
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]<android-developers%[email protected]>
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>>
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to