Android Programming Tutorials

(Romina) #1
Seeking the Proper Level

}

}

Step #4: Choose and Use the Bridge.................................................


With all that behind us, now we need to actually pick the right


implementation of the MobileContactsBridge interface and use it, replacing


our former hard-wired Android 2.x API usage in sendSMS().


One way to determine which implementation to use is to see what SDK we


are running. To do that, we can use the SDK property of the android.os.Build


class (which you will need to add as in import to DetailForm). This contains


a String representation of our API level, with 5 being Android 2.0, 4 being


Android 1.6, and so on. Note that while there is an SDK_INT property that


would save us the String-to-integer conversion, it is not available on


Android 1.5.


So, replace your current implementation of sendSMS() in DetailForm to:


private void sendSMS() {
MobileContactsBridge bridge=buildBridge();
final Cursor c=bridge.getMobileNumbers(this);

DialogInterface.OnClickListener onSMSClicked=
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
c.moveToPosition(position);

noReallySendSMS(c.getString( 2 ));
}
};

new AlertDialog.Builder(this)
.setTitle("Pick a Person")
.setCursor(c, onSMSClicked, bridge.getDisplayNameField())
.show();
}

This delegates the creation of the actual MobileContactsBridge to a


buildBridge() method, which you will also need to add to DetailForm:


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

374
Free download pdf