android - How to code ContactsContract so that it runs in 2.2 and doesn't crash 1.5? -
I have my Android app sees the code to the following piece of a contact which is id, I changed the target to 1.5 right now Given is 2.2, but with a minimum SDK of 3 (1.5) so that I can use the contact contractor in 2.2 and URI in 1.5.
While running on 1.5 device code leads to the following error:
07-27 15: 02: 53.382: Warne / Delvikm (12656): Vifwai: Landroid Unable to resolve static field 25 (CONTENT_URI) in / provider / contacts; $ Contacts;
With Google, I need to use the reflection in this case so that we can get permission to run the application on both versions of Android?
How do I use reflection to use methods of multiple / different versions of the examples, but how can I use it in my own use where I want to use 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", "pressed contact ID button =" + Contact ID;) if (sdk.equals ( "8")) {contactUri = ContentUriskwithAppendedId (Snprkkaktrakt. Contact. CONTENT_URI, ContactId); intent intent = new intent (Intent.ACTION_VIEW, contact URI), early (intent);} other {contactUri = ContentUris.withAppendedId (people. CONTENT_URI, ContactId); effect intended = New intent (Intent.ACTION_VIEW, contact Yuri); startActivity;} sending key events ( New keyEvent (KeyEvent.ACTION_DOWN, KeyEvent.FLAG_SOFT_KEYBOARD)); Dispatch Kiaavent (new Kiivent (Kunjiavent. Akshn_ UP Kiivent. Kekekodiblack));}});
Edit:
An example of how this has been done:
Private constant Yuri CONTENT_URI = Faucet; Private Static String NAME_URI = Faucet; Private static string ID_URI = Faucet; Private Static String NUMBER_URI = Faucet; Private static URI PHONE_LOOKUP = Faucet; Stable int SDK; Static {sdk = new integer (build.VERSION.SDK) .intValue (); If (SDK> = 5) {Class Clause = Class. FORName ("android.provider.ContactsContract $ CommonDataKinds $ Phone"); Class clazz_phonelookup = class. ForName ("android.provider.ContactsContract $ PhoneLookup"); NAME_URI = (string) clazz.getField ("DISPLAY_NAME") find (clazz). ID_URI = (string) find clazz.getField ("_ ID") (clazz). NUMBER_URI = (string) find clazz.getField ("number") (clazz). PHONE_LOOKUP = (Uri) clazz_phonelookup.getField ("CONTENT_FILTER_URI"). Receive (clause); } Hold (Throne T) {Log. ("Reflection", "exception is fixed when CONTENT_URI", T); }} And {CONTENT_URI = Contact. People CONTENT_URI; NAME_URI = People.NAME; ID_URI = People._ID; NUMBER_URI = People.NUMBER; PHONE_LOOKUP = Null; }}
From the documentation, this demo is a parent for two separate SDK-specific Class defines API level 3/4 and classes working for API level 5+.
This helped me to understand the differences between contacts and contacts, and OS api level in Operate Class.
Hope it helps!
Comments
Post a Comment