Android Programming Tutorials

(Romina) #1
Your Friends Seem Remote

At this point, if you recompile each project, you will see they both have


compiler errors – PostMonitor cannot find IPostListener and Patchy cannot


find PostMonitor, for example.


Step #4: Implement and Copy the AIDL..........................................


Now, we need to implement AIDL to represent the interface that the


service exposes to the client and the callback that the client exposes to the


service.


We already almost have the callback AIDL, since AIDL closely resembles


Java interfaces. Move Patchy/src/apt/tutorial/two/IPostListener.java to


Patchy/src/apt/tutorial/IPostListener.aidl, moving it up one directory


and changing the file extension to .aidl. Then, remove the public


keywords, and the AIDL is set. Repeat this process with IPostMonitor.java


as well.


Then, copy the new IPostListener.aidl and IPostMonitor.aidl files to the


corresponding directory in TMonitor (TMonitor/src/apt/tutorial). We also


have to deal with the possibility of an android.os.RemoteException when


using the callback, since the calling process might have unexpectedly


terminated. So, we need to wrap our callback use in a try/catch block as


follows:


private void poll(Account l) {
try {
Twitter client=new Twitter(l.user, l.password);

client.setAPIRootUrl("https://identi.ca/api");

List<Twitter.Status> timeline=client.getFriendsTimeline();

for (Twitter.Status s : timeline) {
if (!seenStatus.contains(s.id)) {
try {
l.callback.newFriendStatus(s.user.screenName, s.text,
s.createdAt.toString());
seenStatus.add(s.id);
}
catch (Throwable t) {
Log.e("PostMonitor", "Exception in callback", t);
}

205
Free download pdf