Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to code ContactsContract so that it runs in 2.2 and doesn't crash 1.5?
    primarykey
    data
    text
    <p>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.</p> <p>However the code leads to the following error when run on 1.5 devices:</p> <pre><code>07-27 15:02:53.382: WARN/dalvikvm(12656): VFY: unable to resolve static field 25 (CONTENT_URI) in Landroid/provider/ContactsContract$Contacts; </code></pre> <hr> <p>From google I have garnered that I need to use reflection in this case to allow the application run on both versions of Android?</p> <p>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?</p> <p>Here is my code:</p> <hr> <pre><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)); } }); </code></pre> <hr> <p>EDIT:</p> <p>Here is an example of how its done:</p> <pre><code>private static Uri CONTENT_URI=null; private static String NAME_URI=null; private static String ID_URI=null; private static String NUMBER_URI = null; private static Uri PHONE_LOOKUP = null; static int sdk; static { sdk=new Integer(Build.VERSION.SDK).intValue(); if (sdk&gt;=5) { try { Class clazz=Class.forName("android.provider.ContactsContract$CommonDataKinds$Phone"); Class clazz_phonelookup=Class.forName("android.provider.ContactsContract$PhoneLookup"); 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); PHONE_LOOKUP =(Uri)clazz_phonelookup.getField("CONTENT_FILTER_URI").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; PHONE_LOOKUP = null; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload