Your SlideShare is downloading. ×
  • Like
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply


  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


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