截图:
上传源代码,供大家参考!
ContactsActivity(Gtalk好友列表)
复制内容到剪贴板代码:import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.provider.Im;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
//import android.widget.Toast;
public class ContactsActivity extends Activity {
private static final int messageSender = 1;
private TextView text = null;
public static final String KEY_Name = "messageName";
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.contacts);
// Run a query against CONTENT_URI = "content://im/contacts"
Cursor cursor=managedQuery(Im.Contacts.CONTENT_URI, null,null, null, null);
TableLayout layout=(TableLayout)findViewById(R.id.contacts_table_layout);
int PRESENCE_STATUS=cursor.getColumnIndex(Im.PresenceColumns.PRESENCE_STATUS);
int NICKNAME=cursor.getColumnIndex(Im.ContactsColumns.NICKNAME);
int USERNAME=cursor.getColumnIndex(Im.ContactsColumns.USERNAME);
int numRows=cursor.count();
cursor.first();
for(int i=0;i<numRows;i++)
{ int presence=(int)cursor.getLong(PRESENCE_STATUS);
String name=cursor.getString(NICKNAME);
String jid=cursor.getString(USERNAME);
if(presence>0) layout.addView(row(new Contact(name,jid,presence)));
cursor.next();
}
}
class Contact
{ String name;
String jid;
int icon;
TextView text=null; // TODO: TEST
Contact(int i) // TEST //
{ name="My Buddy ";//+i
jid="Buddy"+i+"@jabber.net";
icon=R.drawable.icon;
}
Contact(String name,String jid,int presence)
{ this.name=name; //" ("+presence+") "
this.jid=jid;
switch(presence)
{ //case Im.Presence.INVITED: icon=R.drawable.error; break;
case Im.Presence.OFFLINE: icon=R.drawable.icon; break;
case Im.Presence.INVISIBLE: icon=R.drawable.icon; break;
case Im.Presence.DO_NOT_DISTURB: icon=R.drawable.icon; break;
case Im.Presence.AWAY: icon=R.drawable.icon; break;
case Im.Presence.IDLE: icon=R.drawable.icon; break;
case Im.Presence.AVAILABLE: icon=R.drawable.icon; break;
}
}
}
// private void logMessage(CharSequence msg) {
// Toast.makeText(ContactsActivity.this, msg,
// Toast.LENGTH_SHORT).show();
// }
private TableRow row(final Contact contact)
{
TableRow template=(TableRow)findViewById(R.id.contacts_row_layout);
android.view.ViewGroup.LayoutParams layoutParams=template.getLayoutParams();
TableRow layout=new TableRow(this);
layout.setLayoutParams(layoutParams);
ImageView imageView=new ImageView(this);
imageView.setImageResource(contact.icon);
imageView.setPadding(2,6,2,0); // left, top, right, bottom
layout.addView(imageView);//,layoutParams);
text=new TextView(this);
// text.setTag(contact); contact.text=text;
// logMessage("contact name="+contact.name);
text.setText(contact.name);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setFocusable(true);
layout.addView(text);//,layoutParams);
layout.setOnClickListener(textClickListener);
return(layout);
}
private OnClickListener textClickListener=new OnClickListener()
{ public void onClick(View view)
{
createMessage();
}
};
private void createMessage() {
String name = (String) text.getText();
Intent message = new Intent(this,MessageSender.class);
// logMessage("name="+name);
message.putExtra(KEY_Name,name);
startSubActivity(message, messageSender);
}
}
MessageSender (发消息)复制内容到剪贴板代码:import com.google.android.gtalkservice.IChatSession;
import com.google.android.gtalkservice.IGTalkService;
import com.google.android.gtalkservice.IGTalkSession;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.provider.Im;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
public class MessageSender extends Activity implements View.OnClickListener {
private EditText senderEdit = null;
private ListView mesgList = null;
private Button sendButn = null;
private TextView recevieView = null;
IGTalkSession mXmppSession = null;
IGTalkService xmppService;
private String user = null;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.message);
recevieView = (TextView)this.findViewById(R.id.recipient);
senderEdit = (EditText)this.findViewById(R.id.sendText);
mesgList = (ListView)this.findViewById(R.id.listMessages);
sendButn = (Button)this.findViewById(R.id.send);
sendButn.setOnClickListener(this);
Bundle extras = getIntent().getExtras();
if (extras != null) {
user = extras.getString(ContactsActivity.KEY_Name);
// logMessage("***********user************"+user);
if(user != null){
recevieView.setText(user);
Cursor cursor = managedQuery(Im.Messages.CONTENT_URI, null,
null, null, null);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
new String[]{Im.MessagesColumns.BODY},
new int[]{android.R.id.text1});
this.mesgList.setAdapter(adapter);
}
}
}
private void logMessage(CharSequence msg) {
Toast.makeText(MessageSender.this, msg,
Toast.LENGTH_SHORT).show();
}
public void onClick(View view) {
if(view == sendButn ){
try{
xmppService = GtalkActivity.xmppservice;
if(xmppService == null){
logMessage("xmppService is null");
}
try {
mXmppSession = xmppService.getDefaultSession();
} catch (DeadObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(mXmppSession == null){
logMessage("mXmppSession is null");
return;
}
IChatSession ichatsession = mXmppSession.createChatSession(user);
if(ichatsession == null)
{
logMessage("Fail to create chat session. Return.");
return;
}
ichatsession.sendTextMessage(senderEdit.getText().toString());
} catch (DeadObjectException e) {
e.printStackTrace();
}
}
}
}
AndroidGtalk.rar (59.1 KB)
[
本帖最后由 Xsuo 于 2008-6-18 07:35 编辑 ]