Implementing the ViewOnClick Listener

You need to modify the GoogleAPI class to implement the View.OnClickListener. This will allow you to call the onClick( ) method from the Activity's main class when any button is clicked. Normally, this way of implementing the onClick( ) method is only effective when you have numerous buttons on one Activity and want to handle all the onClick calls in one method. However, I felt that you should still see how the method works so you can use it in your own future code. Keep in mind that I am showing this method because it can be a valuable tool in many situations.

public class GoogleAPI extends Activity implements View.OnClickListener { }

Implementing general variables in your Activity is another concept that hasn't been covered previously in this book. You need to establish in this Activity a few general variables that you can work with from multiple methods:

EditText messageText; ListView messageList; IGTalkSession myIGTalkSession; EditText messageTo; Button sendButton;

In your onCreate() method, you will perform your normal initializations. You should assign your layouts to your Views and set IGTalkSession to null. Also, just to add a little bit of interest to your Activity, change the background color of the ListView to gray.

mylGTalkSession = null;

messageText = (EditText) findViewById(R.id.messageText); messageList = (ListView) findViewById(R.id.messageList); messageTo = (EditText) findViewById(R.id.messageTo); sendButton = (Button) findViewById(R.id.btnSend); sendButton.setOnClickListener(this); messageList.setBackgroundColor(Color.GRAY );

Because you are implementing View.OnClickListener from your class, you can set the Send Button's OnClickListener( ) method to this.

The final piece of business to perform in the onCreate( ) method is to bind your service. This process creates the connection that you will use, facilitated by the Google account you established in the Dev Tools, to pass your GTalk messages:

this.bindService(new

Intent().setComponent(GTalkServiceConstants.GTALK_SERVICE_COMPONENT), connection, 0);

In the bindService statement above, one of the parameters you pass to the setComponent() method is connection. This variable represents a ServiceConnection that implements the onServiceConnected( ) and onServiceDisconnected( ) methods. The following code builds the connection that is bound in the previous bindService statement:

private ServiceConnection connection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { try {

myIGTalkSession = IGTalkService.Stub.asInterface(service).getDefaultSession(); } catch (DeadObjectException e) { myIGTalkSession = null;

public void onServiceDisconnected(ComponentName name) { myIGTalkSession = null;

In the onServiceConnected( ) method, you establish a session using the IGTalkService.Stub. If this process fails, you should set the session to null once again. Similarly, in the onServiceDisconnected( ) method, you set the session to null.

Now you can create the code for the class's onClick event. There are several actions that you should perform during each onClick event:

1. Check the database for any messages.

2. Create a ListAdapter from the results of this query and display them to the ListView.

3. Create a ChatSession to the address in the EditView and send your message text.

NOTE

The Android server includes a SQLite database that you can use to hold many Activity-related items and any custom data you feel should be put into it. This database is introduced in depth in Chapter 11.

The following line of code queries the database for any messages sent between you and the messageTo recipient:

Cursor cursor = managedQuery(Im.Messages.CONTENT_URI, null, "contact=\'" + messageTo.getText().toString() + "\'", null, null);

Use the following code to create a ListAdapter from the query results and assign the adapter to the ListView. You have used a similar process in a previous Activity, so it should not look foreign to you.

ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[]{lm.MessagesColumns.BODY}, new int[]{android.R.id.text1}); this.messageList.setAdapter(adapter);

With the messages displayed, the last step is to send your message. The following lines of code create an IChatSession with the specified messageTo address. The message text is then passed over this session to the recipient.

IChatSession chatSession; chatSession =

myIGTalkSession.createChatSession(messageTo.getText().toString(););

chatSession.sendTextMessage(messageText.getText().toStringi } catch (DeadObjectException ex) { mylGTalkSession = null;

When you put it all together, the complete GoogleAPI.java file should look like this:

package android_programmers_guide.GoogleAPI;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.database.Cursor;

import android.os.Bundle;

import android.os.DeadObjectException;

import android.os.IBinder;

import android.provider.Im;

import android.graphics.Color;

import android.view.View;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.ListAdapter;

import android.widget.Button;

import android.widget.SimpleCursorAdapter;

import com.google.android.gtalkservice.IGTalkSession;

import com.google.android.gtalkservice.IGTalkService;

import com.google.android.gtalkservice.GTalkServiceConstants;

import com.google.android.gtalkservice.IChatSession;

public class GoogleAPI extends Activity implements View.OnClickListener {

EditText messageText;

ListView messageList;

IGTalkSession mylGTalkSession;

EditText messageTo;

Button mSend;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); myIGTalkSession = null;

messageText = (EditText) findViewById(R.id.messageText); messageList = (ListView) findViewById(R.id.messageList); messageTo = (EditText) findViewById(R.id.messageTo); mSend = (Button) findViewById(R.id.btnSend); mSend.setOnClickListener(this); messageList.setBackgroundColor(Color.GRAY );

this.bindService(new Intent().setComponent(GTalkServiceConstants.GTALK_SERVICE_COMPONENT), connection, 0); }

private ServiceConnection connection = new ServiceConnection() {

public void onServiceConnected(ComponentName name, IBinder service) { try {

mylGTalkSession = IGTalkService.Stub.aslnterface(service).getDefaultSession(); } catch (DeadObjectException e) { mylGTalkSession = null;

public void onServiceDisconnected(ComponentName name) { mylGTalkSession = null;

public void onClick(View view) {

Cursor cursor = managedQuery(Im.Messages.CONTENT_URI, null,

"contact=\'" + messageTo.getText().toString() + "\'",

ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[]{lm.MessagesColumns.BOD7}, new int[]{android.R.id.textl}); this.messageList.setAdapter(adapter);

IChatSession chatSession; chatSession =

myIGTalkSession.createChatSession(messageTo.getText().toString());

chatSession.sendTextMessage(messageText.getText().toString()) } catch (DeadObjectException ex) { mylGTalkSession = null;

null, null)

+1 0

Post a comment