环境:为了从一对一/一对多设备不间断地传递消息,我在后台运行XMPPService作为STICKY服务。 为了接收消息,我有PacketListener,只要数据包到达设备就会触发。我使用asmack库进行客户端实现,将OpenFire用作我的XMPP服务器。
public class XMPPService extends Service implements INetworkReceiver {
private XMPPConnection mXmppConnection;
private XMPPMethods xmpp;
private DatabaseHandler db;
private UserDetails user;
private SmackAndroid smack;
private Registrations registrations;
private static final String TAG="XMPPService";
@Override
public IBinder onBind(final Intent intent) {
return new LocalBinder<XMPPService>(this);
}
@Override
public void onCreate() {
super.onCreate();
registrations=new Registrations(getApplicationContext());
xmpp=XMPPMethodsImpl.getInstance(getApplicationContext());
db=DatabaseHandler.getInstance(getApplicationContext());
if(db!=null){
user=db.getUserDetails();
}
createConnection();
}
public void createConnection()
{
ConnectionConfiguration connConfig = new ConnectionConfiguration(
AppConstants.HOST, AppConstants.PORT);
connConfig.setSASLAuthenticationEnabled(false);
connConfig.setDebuggerEnabled(true);
connConfig.setReconnectionAllowed(true);
connConfig.setSecurityMode(SecurityMode.disabled);
mXmppConnection = new XMPPConnection(connConfig);
Thread thread=new Thread(new Runnable() {
private Handler handler=new Handler();
@Override
public void run() {
try {
if(mXmppConnection!=null && !mXmppConnection.isConnected()){
mXmppConnection.connect();
Log.i("XMPPServiceAsync",
"Connected to " + mXmppConnection.getHost());
xmpp.setConnection(mXmppConnection);
}
} catch (XMPPException e) {
e.printStackTrace();
}
//mXmppConnection.addConnectionListener(new XmppConnectionListener());
handler.post(new Runnable() {
@Override
public void run() {
try {
if(db!=null && user!=null && mXmppConnection!=null && !mXmppConnection.isAuthenticated()){
if(user.getUserName()!=null && !user.getUserName().isEmpty()){
mXmppConnection.login(user.getUserName(), "Tp@!");
xmpp.setPresence(2, mXmppConnection);
xmpp.setConnection(mXmppConnection);
Log.i("XMPPServiceAsync",
"Logged in as " + mXmppConnection.getUser());
}
}
}catch (IllegalStateException e) {
e.printStackTrace();
} catch (XMPPException ex) {
ex.printStackTrace();
}
if(mXmppConnection.isConnected()){
xmpp.rejoinGroups();
registrations.registerMessageListener(mXmppConnection);
registrations.registerPacketListener(mXmppConnection);
registrations.registerMultiUserInvitationListener(mXmppConnection, db, user);
registrations.registerPingListener(mXmppConnection);
}
}
});
}
});
if (thread.getState() == Thread.State.NEW )
{
Log.i(TAG,"Thread State: "+ thread.getState()+"");
thread.start();
}
}
@Override
public int onStartCommand(final Intent intent, final int flags,
final int startId) {
//rejoining rooms
try{
Log.v("XMPP Connection before rejoining",mXmppConnection+"");
if(mXmppConnection.isConnected()){
registrations.registerMessageListener(mXmppConnection);
registrations.registerPacketListener(mXmppConnection);
registrations.registerMultiUserInvitationListener(mXmppConnection, db, user);
registrations.registerPingListener(mXmppConnection);
}
}catch(Exception e){
e.printStackTrace();
}
return Service.START_STICKY;
}
@Override
public boolean onUnbind(final Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void reEstablishConnection() {
createConnection();
}
}
问题:当设备闲置一段时间后,设备与XMPP服务器断开连接,连接变为空,这违反了XMPP协议的规范,该协议规定XMPP服务器与设备保持持久连接,直到我们强行退出。连接变为null的原因尚不清楚。当连接变为null时,PacketListener将停止工作。
我尝试了什么:
有人可以查看此内容。
答案 0 :(得分:1)
当设备保持空闲一段时间后,设备将断开与XMPP服务器的连接并且连接变为空
最有可能的是,您的流程已终止。 This is perfectly normal
这违反了XMPP协议的规范,该规范声明XMPP服务器与设备保持持久连接,直到我们强行注销。
然后,没有移动设备可以遵守XMPP协议规范,因为无法保证和通用Internet访问。此外,用户可以在用户想要的时候摆脱您的流程。
实现了用于连接服务器和初始化侦听器的粘性服务。
这将导致您的流程和服务在Android终止流程后的某个时间重新启动。在您的进程终止和重新启动进程之间的时间窗口期间,您将不会与XMPP服务器进行通信。
答案 1 :(得分:0)
commonsware说了什么。 START_STICKY只是意味着Android可以在需要资源时自由终止您的服务。一段时间后它将重新启动您的服务。但是,当发生XMPP连接时,您有责任重新建立它。