2008년 6월 7일 토요일

Android GTalk Client

GTalkDataMessageSender

Video

public class GTalkDataMessageSender extends Activity {
private static final String LOG_TAG = "GTalkServiceSample";
IGTalkSession mGTalkSession = null;
EditText mUsernameField;
Button mSendButton;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.gtalk_data_message_sender);
mUsernameField = (EditText)findViewById(R.id.username);
mUsernameField.setOnClickListener(mOnClickListener);
mUsernameField.requestFocus();
mSendButton = (Button)findViewById(R.id.send);
mSendButton.setOnClickListener(mOnClickListener);
mSendButton.setEnabled(false);
bindGTalkService();
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mConnection);
}
private void bindGTalkService() {
bindService((new Intent()).setComponent(
com.google.android.gtalkservice.GTalkServiceConstants.GTALK_SERVICE_COMPONENT),
mConnection, 0);
}
private boolean isValidUsername(String username) {
if (TextUtils.isEmpty(username)) {
return false;
}
if (username.indexOf('@') == -1) {
return false;
}
return true;
}
private Intent getIntentToSend() {

// intent만들고 메시지를 대입.
//I make intent and input a message

Intent intent = new Intent(GTalkDataMessageReceiver.ACTION);
intent.putExtra("poke", "Hi, I am Sam.");
intent.putExtra("question", "would you like to eat green eggs and ham?");
return intent;
}
private void showMessage(CharSequence msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the GTalkService has been
// established, giving us the service object we can use to
// interact with the service. We are communicating with our
// service through an IDL interface, so get a client-side
// representation of that from the raw service object.
IGTalkService GTalkService = IGTalkService.Stub.asInterface(service);
try {
mGTalkSession = GTalkService.getDefaultSession();
if (mGTalkSession == null) {
// this should not happen.
showMessage(getText(R.string.gtalk_session_not_found));
return;
}
} catch (DeadObjectException ex) {
Log.e(LOG_TAG, "caught " + ex);
showMessage(getText(R.string.found_stale_gtalk_service));
}
mSendButton.setEnabled(true);
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mGTalkSession = null;
mSendButton.setEnabled(false);
}
};
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
public void onClick(View v) {
if (v == mUsernameField) {
mSendButton.requestFocus();
} else {
// use GTalkService to send data message to someone
String username = mUsernameField.getText().toString();
if (!isValidUsername(username)) {
showMessage(getText(R.string.invalid_username));
return;
}
if (mGTalkSession == null) {
showMessage(getText(R.string.gtalk_service_not_connected));
return;
}
try {

//모든 게 중요하지만 특히 이 한 줄의 코드가
//이 프로그램의 핵심이다.
//따라서 이 부분을 이해한다면
//이 프로그램을 이해한다고 볼 수 있다.
//Everything is important, but a code of this one line is the core of this program particularly.

mGTalkSession.sendDataMessage(username, getIntentToSend());

} catch (DeadObjectException ex) {
Log.e(LOG_TAG, "caught " + ex);
showMessage(getText(R.string.found_stale_gtalk_service));
mGTalkSession = null;
bindGTalkService();
}
}
}
};
}

댓글 없음:

댓글 쓰기

-


Sidewinder


World


FishMusic


LaughingBaby