Android XMPP例子(Openfire+asmack+spark)
立即下载
资源介绍:
Android XMPP例子(Openfire+asmack+spark)
package com.xmpp.client;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.filetransfer.FileTransfer;
import org.jivesoftware.smackx.filetransfer.FileTransfer.Status;
import org.jivesoftware.smackx.filetransfer.FileTransferListener;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
import com.xmpp.client.util.TimeRender;
import com.xmpp.client.util.XmppTool;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class FormClient extends Activity {
private MyAdapter adapter;
private List listMsg = new ArrayList();
private String pUSERID;
private EditText msgText;
private ProgressBar pb;
public class Msg {
String userid;
String msg;
String date;
String from;
public Msg(String userid, String msg, String date, String from) {
this.userid = userid;
this.msg = msg;
this.date = date;
this.from = from;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.formclient);
this.pUSERID = getIntent().getStringExtra("USERID");
ListView listview = (ListView) findViewById(R.id.formclient_listview);
listview.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
this.adapter = new MyAdapter(this);
listview.setAdapter(adapter);
this.msgText = (EditText) findViewById(R.id.formclient_text);
this.pb = (ProgressBar) findViewById(R.id.formclient_pb);
//message listener
ChatManager cm = XmppTool.getConnection().getChatManager();
final Chat newchat = cm.createChat("test2@sam", null);
cm.addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean able) {
chat.addMessageListener(new MessageListener() {
@Override
public void processMessage(Chat chat2, Message message) {
//message from user [test2@sam]
if(message.getFrom().contains("test2@sam")){
String[] args = new String[] { "test2", message.getBody(), TimeRender.getDate(), "IN" };
android.os.Message msg = handler.obtainMessage();
msg.what = 1;
msg.obj = args;
msg.sendToTarget();
}else{
// orther user / group / admin of the openfire
// do work...
}
}
});
}
});
//send file
Button btattach = (Button) findViewById(R.id.formclient_btattach);
btattach.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(FormClient.this, FormFiles.class);
startActivityForResult(intent, 2);
}
});
//send message
Button btsend = (Button) findViewById(R.id.formclient_btsend);
btsend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String msg = msgText.getText().toString();
if(msg.length() > 0){
listMsg.add(new Msg(pUSERID, msg, TimeRender.getDate(), "OUT"));
adapter.notifyDataSetChanged();
try {
newchat.sendMessage(msg);
} catch (XMPPException e) {
e.printStackTrace();
}
}
msgText.setText("");
}
});
//receive file
FileTransferManager fileTransferManager = new FileTransferManager(XmppTool.getConnection());
fileTransferManager.addFileTransferListener(new RecFileTransferListener());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==2 && resultCode==2 && data!=null){
String filepath = data.getStringExtra("filepath");
if(filepath.length() > 0){
sendFile(filepath);
}
}
}
private void sendFile(String filepath) {
// ServiceDiscoveryManager sdm = new ServiceDiscoveryManager(connection);
final FileTransferManager fileTransferManager = new FileTransferManager(XmppTool.getConnection());
final OutgoingFileTransfer fileTransfer = fileTransferManager.createOutgoingFileTransfer("test2@sam/Spark 2.6.3");
final File file = new File(filepath);
try {
fileTransfer.sendFile(file, "Sending");
} catch (Exception e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
try{
while (true) {
Thread.sleep(500L);
Status status = fileTransfer.getStatus();
if ((status == FileTransfer.Status.error)
|| (status == FileTransfer.Status.complete)
|| (status == FileTransfer.Status.cancelled)
|| (status == FileTransfer.Status.refused)) {
handler.sendEmptyMessage(4);
break;
}else if(status == FileTransfer.Status.negotiating_transfer){
//..
}else if(status == FileTransfer.Status.negotiated){
//..
}else if(status == FileTransfer.Status.initial){
//..
}else if(status == FileTransfer.Status.negotiating_stream){
//..
}else if(status == FileTransfer.Status.in_progress){
handler.sendEmptyMessage(2);
long p = fileTransfer.getBytesSent() * 100L / fileTransfer.getFileSize();
android.os.Message message = handler.obtainMessage();
message.arg1 = Math.round((float) p);
message.what = 3;
message.sendToTarget();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private FileTransferRequest request;
private File file;
class RecFileTransferListener implements FileTransferListener {
@Override
public void fileTransferRequest(FileTransferRequest prequest) {
System.out.println("The file received from: " + prequest.getRequestor());
file = new File("mnt/sdcard/" + prequest.getFileName());
request = prequest;
handler.sendEmptyMessage(5);
}
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
String[] args = (String[]) msg.obj;
listMsg.add(new Msg(args[0], args[1], args[2], args[3]));
adapter.notifyDataSetChanged();
break;
case 2:
if(pb.getVisibility()==View.GONE){
pb.setMax(100);
pb.setProgress(0);
pb.setVisibility(View.VISIBLE);
}
break;
case 3:
pb.setProgress(msg.arg1);
break;
case 4:
pb.setVisibility(View.GONE);
break;
case 5:
final IncomingFileTransfer infiletransfer = request.accept();
AlertDialog.Builder builder = new AlertDialog.Builder(FormClient.this);
builder.setTitle("receive file")
.setCancelable(false)
.setPositiveButton("Receive",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
infiletransfer.recieveFile(file);
} catch (XMPPException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(2);
Timer timer = new Timer();
TimerTask updateProgessBar = new TimerTask() {
public void run() {
if ((infiletransfer.getAmountWritten() >= request.getFi
资源文件列表:
AdXmpp.zip 大约有103个文件