android 实现xmppconnection服务

标签 android service xmpp asmack

我正在尝试使用 asmack 和 Openfire 创建一个基本的聊天应用程序。 我为 XMPPConnection 创建了一个绑定(bind)服务,每个 Activity 都绑定(bind)到它。

每当我尝试绑定(bind)到服务时,都会出现很长的延迟。我知道 bindService 是异步的,但在开始寻找其他问题之前,我想确定我的服务实现是正确的。

我在 onCreate 方法中绑定(bind)我的服务,并尝试在 onStart 中访问连接。

我对此仍然很陌生,但我怀疑我在线程方面做了一些错误的事情。我的应用程序现在运行的方式,仅当我尝试从 OnClickListener 访问 mBound 变量时才返回 true。 Listener 中发生的什么事情会产生如此大的差异?我尝试查找 OnClick 方法的代码,但找不到。

我的 XMPPConnectionService 是这样的:

包com.example.smack_text;

import java.io.File;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class XMPPService extends Service{

XMPPConnection connection;
//  private final IBinder mBinder = new LocalBinder();


@Override
public void onCreate(){
super.onCreate();
Log.d("service","created");
}

/**
 * Class used for the client Binder.  Because we know this service always
 * runs in the same process as its clients, we don't need to deal with IPC.
 */
@Override
public IBinder onBind(Intent intent) {
Log.d("sevice","bound");
LocalBinder mBinder = new LocalBinder (this);
return mBinder;
}

public class LocalBinder extends Binder {
XMPPService service;

public LocalBinder (XMPPService service)
{
this.service = service;
}

public XMPPService getService (){
return service;
}

//          XMPPService getService() {
//              return XMPPService.this;
//          }
}

public void connect(final String user, final String pass) {
Log.d("Xmpp Alex","in service");

ConnectionConfiguration config = new ConnectionConfiguration("10.0.2.2",5222);

//          KEYSTORE SETTINGS
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
config.setTruststoreType("AndroidCAStore");
config.setTruststorePassword(null);
config.setTruststorePath(null);
} 
else {
config.setTruststoreType("BKS");
String path = System.getProperty("javax.net.ssl.trustStore");
if (path == null)
path = System.getProperty("java.home") + File.separator + "etc"
       + File.separator + "security" + File.separator
       + "cacerts.bks";
    config.setTruststorePath(path);
} 

//          Create XMPP Connection

connection = new XMPPConnection(config);

new Thread(new Runnable() {
@Override
public void run() {

try {
connection.connect();
connection.login(user, pass);
if(connection.isConnected()){
Log.d("Alex", "connected biatch!");
}
else{
Log.d("Alex","not connected");
}


} catch (XMPPException e) {
e.printStackTrace();
}
}
}).start();
}
public void disconnect(){
if(connection.isConnected()){
connection.disconnect();
}  
else{
Toast.makeText(getApplicationContext(), "not connected", Toast.LENGTH_LONG).show();
}
}
}

最佳答案

我使用 Asmack 实现了 Android 聊天。

我创建了一个服务。 该服务有一个带有 XmppConnection 的全局变量。 一开始我使用线程进行连接和登录。 然后我为登录用户设置 VCard,设置 rosterListener 最后设置connection.addPacketListener 我使用 BroadcastReceiver Activity 端更新 Activity ,并且

@Override
    public IBinder onBind(Intent arg0) {

        return mBinderXmpp;
    }

    public class BinderServiceXmpp extends Binder {
        ServiceXmpp getService() {
            return ServiceXmpp.this;
        }
    }


    private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            DisplayInfo();
            handler.postDelayed(this, 2000); // 2 segundos
        }
    };
    private void DisplayInfo() {
        isRunning = true; // flag to know if service is running
        Intent tempIntent;
        tempIntent = new Intent(BROADCAST_ACTION);
        tempIntent.putExtra("UPDATE_OPTION", UPDATE_ACTION);
        sendBroadcast(tempIntent);


    }

关于android 实现xmppconnection服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15587875/

相关文章:

Android 应用程序无法针对小型设备正确缩放

android - android 推送通知分组不起作用

java - MenuInflater 不工作 R 无法解析为变量

tomcat - 服务 Liferay 中的 java.lang.NoSuchMethodError

android - 如何绑定(bind) startActivity() 需要 FLAG_ACTIVITY_NEW_TASK?

android - 使用 github 操作和问题签署发布 apk

android - 测试使用 Messenger 的远程服务

java - 使用 asmack for android 在 XMPP 数据包中的消息标记中添加自定义属性?

java - Smack API 抛出 item-not-found(404) 异常

ios - XMPP 上次事件时间以奇怪的形式显示。如何解决?