Android Programming Tutorials

(Romina) #1
Opening a JAR

}

return(client);
}

synchronized private void resetClient() {
client=null;
}

Finally, in updateStatus(), get rid of the references to the former widgets


and use getClient() to lazy-create our Twitter object, as follows:


private void updateStatus() {
try {
getClient().updateStatus(status.getText().toString());
}
catch (Throwable t) {
Log.e("Patchy", "Exception in updateStatus()", t);
goBlooey(t);
}
}

The net is that we will use one Twitter object, on our first status update,


until the application is closed or the user changes credentials.


If you rebuild and reinstall the application, you should now be able to


update your status, yet only enter the status information, using the user


name and password from the preferences.


Here is the complete implementation of Patchy after completing this


tutorial:


package apt.tutorial.two;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.util.Log;
import android.widget.Button;

172
Free download pdf