我正在开发基于用户的androaid应用程序。我想为用户添加相互通信的选项。另外我想让用户在网上看到谁。 我使用android作为客户端。而python作为服务器端。 我可以说它不简单,但我不知道如何开始。所以我会给每一个帮助。 谢谢
答案 0 :(得分:0)
您需要在Android应用程序中包含XMPP聊天客户端。最受欢迎的是asmack。它实际上是一个使用JNI转换为jar库的C ++库。您在libs文件夹中包含asmack jar文件。然后,您只需从org.jivesoftware.smack命名空间导入必要的类。
https://github.com/Flowdalic/asmack
在服务器端,您需要一个像OpenFire这样的聊天服务器。在Linux上设置非常简单,您不必编写任何Python代码以允许Android客户端连接到它。
http://www.igniterealtime.org/projects/openfire/
这是我写的与聊天服务器通信的服务。
package com.qeala.android;
import java.util.ArrayList;
import java.util.List;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Message;
import android.os.Handler;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
public class XMPPService extends Service {
private static String TAG = "XMPPService";
private int mStartMode;
private IBinder mBinder;
private boolean mAllowRebind;
public Handler mHandler;
public static final int MSG_ON_MESSAGE = 10;
public static final int MSG_ON_CONNECTED = 11;
public static final String ACTION_MESSAGE = "ACTION_MESSAGE";
public static final String ACTION_CONNECTED = "ACTION_CONNECTED";
private static final String SERVER_URL = "yourserver.com"
private Connection mConnection;
private ChatManager mChatManager;
private Chat mChat;
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
mStartMode = START_STICKY_COMPATIBILITY;
mBinder = new LocalBinder();
mAllowRebind = false;
mHandler = new Handler(getMainLooper()){
public void handleMessage (Message msg){
Intent intent = new Intent();
switch (msg.what) {
case MSG_ON_MESSAGE:
String message = (String)msg.obj;
intent.setAction(ACTION_MESSAGE);
intent.putExtra("message", message);
sendBroadcast(intent);
break;
case MSG_ON_CONNECTED:
intent.setAction(ACTION_CONNECTED);
sendBroadcast(intent);
break;
}
}
};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();
Log.d(TAG, String.format("onStartCommand:%s:%d:%d", action, flags, startId));
}
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind");
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
Log.d(TAG, "onRebind");
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
}
public class LocalBinder extends Binder {
public XMPPService getServerInstance() {
return XMPPService.this;
}
}
public void sendMessage(final String message){
new Thread(new Runnable(){
@Override
public void run() {
if(mConnection != null){
if(mConnection.isConnected()){
if(mChat != null){
try {
Log.d(TAG, String.format("SEND: %s", message));
mChat.sendMessage(message);
} catch (XMPPException e) {
Log.e(TAG, e.toString());
}
}else{
Log.d(TAG, "mChat is null");
}
}else{
Log.w(TAG, "mConection is not connected");
}
}else{
Log.w(TAG, "mConection is null");
}
}
}).start();
}
public void connect(final String jid){
Log.d(TAG, String.format("connect %s", jid));
new Thread(new Runnable(){
@Override
public void run() {
try{
Connection.DEBUG_ENABLED = true;
mConnection = new XMPPConnection(SERVER_URL);
mConnection.connect();
mConnection.loginAnonymously();
mChatManager = mConnection.getChatManager();
mChat = mChatManager.createChat(jid, new MessageListener() {
public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) {
String body = message.getBody();
Log.d(TAG, String.format("RECEIVE: %s", body));
Message msg = mHandler.obtainMessage(MSG_ON_MESSAGE, body);
mHandler.sendMessage(msg);
}
});
Message msg = mHandler.obtainMessage(MSG_ON_CONNECTED);
mHandler.sendMessage(msg);
}catch(Exception e){
Log.e(TAG, e.toString());
}
}
}).start();
}
}