Notes

329 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
329
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Notes

  1. 1. Comp194-MA
  2. 2. Threads! <ul><li>Android uses the same threading system as traditional Java applications. </li></ul><ul><li>Using threads allows developers to complete computational intensive tasks in the background of their application. </li></ul><ul><li>The main benefit of this is that the UI will remain responsive while the thread is running. </li></ul>
  3. 3. Thread code <ul><li>Creating a thread is simple: </li></ul><ul><li>Thread t = new Thread() { </li></ul><ul><li>public void run(){ </li></ul><ul><li>// do things </li></ul><ul><li>}}; </li></ul><ul><li>If you instantiate a thread as a local variable, it will have access to all of the properties and methods of the containing class. </li></ul><ul><li>In order to make an operation thread safe, surround the statement with a synchronize{} block. </li></ul>
  4. 4. Messages from threads <ul><li>On of the “contracts” of the Android SDK is that only the UI thread will manipulate UI elements. </li></ul><ul><li>As a consequence, you need to pass messages between your new threads and the UI thread. </li></ul><ul><li>The handler object accepts messages and then can deal with them. </li></ul>
  5. 5. Handlers and messages <ul><li>In your activity class place: </li></ul><ul><li>private final Handler handler = new Handler(){ </li></ul><ul><li>public void handleMessage(Message msg) { </li></ul><ul><li>// do different things depending on the message </li></ul><ul><li>} </li></ul><ul><li>Create a thread inside the activity somewhere: </li></ul><ul><li>Thread t = new Thread() { </li></ul><ul><li>public void run(){ </li></ul><ul><li>// do things </li></ul><ul><li>// send a message to the handler </li></ul><ul><li>handler.sendEmptyMessage(2); </li></ul><ul><li>}}; </li></ul>
  6. 6. Misc. Goodies <ul><li>Code to create a contact through an intent: </li></ul><ul><li>private void createContact(){ </li></ul><ul><li>ContentValues values = new ContentValues(); </li></ul><ul><li>Uri phoneUri = null; </li></ul><ul><li>values.put(Contacts.People.NAME, &quot;Matt Daum&quot;); </li></ul><ul><li>values.put(Contacts.People.STARRED,0); </li></ul><ul><li>values.put(People.NOTES, &quot;&quot;); </li></ul><ul><li>//Add Phone Numbers </li></ul><ul><li>Uri uri = Contacts.People.createPersonInMyContactsGroup(getContentResolver(), values); </li></ul><ul><li>long contactId = Long.parseLong(uri.getLastPathSegment()); </li></ul><ul><li>phoneUri = Uri.withAppendedPath(uri, Contacts.People.Phones.CONTENT_DIRECTORY); </li></ul><ul><li>values.clear(); </li></ul><ul><li>values.put(Contacts.Phones.TYPE, Phones.TYPE_MOBILE); </li></ul><ul><li>values.put(Contacts.Phones.NUMBER, &quot;(201) 555-5151&quot;); </li></ul><ul><li>Uri phone = getContentResolver().insert(phoneUri, values); </li></ul><ul><li>} </li></ul>
  7. 7. Misc. Info <ul><li>The Android platform supports including external JARs within a project. </li></ul><ul><li>However, this creates some complications because the JAR must first be successfully converted to run on the Dalvik JVM. </li></ul><ul><li>Additionally, a few key core Java packages are not implemented on Android which may cause some JARs to fail. </li></ul><ul><li>A final note is that due to the scarce memory on a cell phone, some JARs might just be to big to work. </li></ul>
  8. 8. Facebook+Java <ul><li>I tried porting the FB4J (http://sourceforge.net/projects/fb4j/) client to Android and failed miserably. </li></ul><ul><li>FB4J introduced so many external dependencies that I had to try and build from source. </li></ul><ul><li>The PropertyDescriptor was the nail in the coffin. I couldn’t find an implementation of java.beans.PropertyDescriptor for Android. </li></ul>
  9. 9. Project? <ul><li>Not being able to use the FB4J library seriously delayed development of the project. </li></ul><ul><li>Things are finally looking up though. </li></ul><ul><li>I’m using this client - http://me.sylvek.net/index.php?2009/02/11/434-api-facebook-with-android for Facebook </li></ul><ul><li>More info soon! </li></ul>

×