简体   繁体   中英

Integrating Twitter in an Android application

I want a simple example of implementing Twitter in our application. I would prefer that it not be browsable; it should open on our application area only. After login the user can post tweets on his/her account.

Good question that leaves very much room for counter-questions :-)

I see at least two ways to walk down the path (note that I don't know much about twitter or how it's used):

  1. You synchronize twitter data ("tweets"?!?) on the phone for later viewing.

  2. You always view a snapshot of the current tweets on a given channel and don't store anything (except for user credentials).

As of the first alternative you'd typically want to synchronize a SQLite database (perhaps with a custom content provider ) on the target with data from a twitter channel on the twitter web servers (you can read up a bit on what the public twitter API looks like here ).

This synchronization would be done by a background service on the phone. Your actual GUI would not communicate with this service itself, it would rather read data from (and only from) the local SQLite database. This way your GUI wouldn't depend on network latency, data traffic or data availability from the web. It would only depend on database connectivity on your local target. Make sure you run your service in a separate thread. By default it will run in the main thread (aka the "GUI-thread").

You could pass an entry to the AlarmManager that would wake up your background service every now and then; the service would cache twitter data in the database and then kill itself (to save resources).

The second alternative wouldn't necessary require the database caching layer (however recommended to get rid of the web dependencies in your GUI layer, the database would then only contain the latest data, old data would be overwritten regardless if the user have read it or not).

In both alternatives you'd probably want to store some basic user info, like user name and password. You could store these values in a SQLite database or if you want to keep it simple: in the Shared Preferences infrastructure.

You would most likely also need to read and parse XML data from the web. This reading and parsing would be done by the service layer (remember: run it in a separate thread to avoid lagging the UI or even getting a Application Not Responding time-out).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM