0

Im developing a user based androaid app. And I want to add the option for users to comunicate with each other. In addition I want to allow users to see whos online. I using android as client side. And python as server side. I can lmagen its not simple, but I have no clue how to start. So I will appretiate every help. Thank you

Shmuli
  • 155
  • 2
  • 13

1 Answers1

0

You'll need to include an XMPP chat client in your Android application. The most popular one is asmack. It's actually a C++ library converted to a jar library using JNI. You include the asmack jar file in your libs folder. Then you just import the necessary classes from the org.jivesoftware.smack namespace.

https://github.com/Flowdalic/asmack

On the server side you'll need a chat server like OpenFire. It's pretty easy to setup on Linux and you won't have to write any Python code to allow the Android client to connect to it.

http://www.igniterealtime.org/projects/openfire/

Here's a service that I wrote to communicate with the chat server.

XMPPService.java

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();
  }

}
itsben
  • 1,017
  • 1
  • 6
  • 11