group_2.ppt

351 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
351
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

group_2.ppt

  1. 1. P2P communication Using the GTalk Service API
  2. 2. Introduction <ul><li>Peer-to-Peer communication highly used in mobile devices. </li></ul><ul><li>Very efficient to when certain factors such as battery, mobility and any other external costs are considered. </li></ul><ul><li>Short Message Service (SMS)? </li></ul><ul><ul><li>Ideal for two very remote devices (satellite?) </li></ul></ul><ul><ul><li>Slow </li></ul></ul><ul><ul><li>Expensive </li></ul></ul><ul><ul><li>Might not carry enough data into one SMS (even more expensive) </li></ul></ul>
  3. 3. GTalkService Basics <ul><li>Very similar to the Java Listener and Handler technique </li></ul><ul><li>Mainly consists of: </li></ul><ul><ul><li>Several Intents </li></ul></ul><ul><ul><ul><li>used to send/receive data </li></ul></ul></ul><ul><ul><ul><li>initialize the service connection </li></ul></ul></ul><ul><ul><li>A class implementing the ServiceConnection interface </li></ul></ul><ul><ul><ul><li>Used once the connection is established </li></ul></ul></ul><ul><ul><li>A class extending the IntentReceiver abstract class </li></ul></ul><ul><ul><ul><li>Used when the message is received </li></ul></ul></ul><ul><ul><li>An IGTalkService </li></ul></ul><ul><ul><ul><li>Used to extract a GTalk session ( IGTalkSession ) </li></ul></ul></ul><ul><ul><li>An IGtalkSession </li></ul></ul><ul><ul><ul><li>Used to send the data using sendMessage(String, Intent) </li></ul></ul></ul>
  4. 4. ServiceConnection-implementing class <ul><li>Must be implemented to specify what to do during certain connection events </li></ul><ul><li>Two methods </li></ul><ul><ul><li>onServiceConnected(ComponentName, IBinder) </li></ul></ul><ul><ul><ul><li>Invoked by the Android platform once the requested service connection is established </li></ul></ul></ul><ul><ul><li>onServiceDisconnected(ComponentName) </li></ul></ul><ul><ul><ul><li>Invoked by the Android platform once the connection is lost (legally/illegally) </li></ul></ul></ul>
  5. 5. ServiceConnection continued … <ul><li>onServiceConnected(ComponentName name, IBinder service) </li></ul><ul><ul><li>IGTalkService gtalk_service = IGTalkService.Stub.asInterface(service); </li></ul></ul><ul><ul><li>IGTalkSession session = gtalk_service.getDefaultSession(); </li></ul></ul><ul><li>onServiceDisconnected(ComponentName name) </li></ul><ul><ul><li>Session = null; //no point in keeping a non-connected session instance </li></ul></ul>
  6. 6. Main procedure <ul><li>Initialize two instances (more on these later on): </li></ul><ul><ul><li>A ServiceConnection, and </li></ul></ul><ul><ul><li>An IntentReceiver </li></ul></ul><ul><li>Request a GTalk Service by binding it to your Activity </li></ul><ul><ul><li>Create a new Intent </li></ul></ul><ul><ul><li>Set the Intent’s component to GTalkService.GTALK_SERVICE_COMPONENT </li></ul></ul><ul><ul><li>Bind the intent using bindService(Intent, ServiceConnection, int); </li></ul></ul><ul><li>Register the IntentReceiver using registerReceiver(IntentReceiver, IntentFilter) </li></ul><ul><li>The platform will automatically invoke the onServiceConnected(ContextName, IBinder) method </li></ul>
  7. 7. Sending a message… <ul><li>Using the recently extracted session, use the sendMessage(String, Intent); </li></ul><ul><ul><li>Intent msg = new Intent(); </li></ul></ul><ul><ul><li>msg.setAction(GTalkDataMessageReceiver.ACTION);//to be explained on the next slide </li></ul></ul><ul><ul><li>msg.putExtra(“1”, “Line 1”); </li></ul></ul><ul><ul><li>msg.putExtra(“2”, 34); </li></ul></ul><ul><ul><li>session.sendDataMessage(“john.doe”, msg); </li></ul></ul>
  8. 8. IntentReceiver – abstract class <ul><li>Must implement one particular abstract method: onReceiveIntent(Context, Intent) </li></ul><ul><ul><li>Invoked by the Android platform once an intent is received from some external entity </li></ul></ul><ul><ul><li>Passes the Intent which was sent by the sender </li></ul></ul><ul><ul><li>Must make sure the incoming Intent is a GTalk message sent by the sender </li></ul></ul>
  9. 9. IntentReceiver continued … <ul><li>onReceiveIntent(Context context, Intent intent) </li></ul><ul><ul><li>if (intent.getAction().equals(GTalkDataMessageReceiver.ACTION)) {//point 2} </li></ul></ul><ul><ul><li>Bundle b = intent.getExtras(); </li></ul></ul><ul><ul><li>b.getString(“1”); //returns “Line 1” </li></ul></ul><ul><ul><li>b.getInt(“2”); //returns 32 </li></ul></ul>

×