В службе вы можете проверить соединение и отключить его, а затем попытаться снова войти в систему. Пожалуйста см. Ниже code.Hope это поможет вам разрешить вашу проблему.
public class MyService extends Service {
public static final String ACTION_LOGGED_IN = "chatapp.loggedin";
private final LocalBinder binder = new LocalBinder();
XMPP xmppConnection;
private MessageHandler messageHandler;
private static String TAG = "MyService";
private void onLoggedIn() {
FromMatchesFilter localFromMatchesFilter = null;
try {
localFromMatchesFilter = new FromMatchesFilter(JidCreate.domainBareFrom(Domainpart.from("[email protected]" + Common.SERVER_HOST)), false);
} catch (XmppStringprepException e) {
e.printStackTrace();
}
if(messageHandler == null){
messageHandler = new MessageHandler(this);
}
XMPP.getInstance().addStanzaListener(this, messageHandler);
XMPP.getInstance().addChatListener(this, new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
}
});
XMPP.getInstance().configureSrvDeliveryManager(this);
if(XMPP.getInstance().isFileTransferNegotiatorServiceEnabled()){
PurplkiteLogs.logError(TAG, "File Transfer Service is enabled");
}
FileTransferNegotiator.IBB_ONLY = true;
}
public static boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager
.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(
service.service.getClassName())) {
return true;
}
}
return false;
}
public XMPP XMPP() {
return this.xmppConnection;
}
public IBinder onBind(Intent paramIntent) {
return this.binder;
}
Handler holdConnectionHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
PurplkiteLogs.logInfo(TAG, "Service Handler Running");
checkUserLogin();
holdConnectionHandler.sendEmptyMessageDelayed(0, 10 * 1000);
}
};
private void checkUserLogin() {
String user = AppSettings.getUser(MyService.this);
XMPPTCPConnection connection = XMPP.getInstance().getConnection(MyService.this);
if(connection != null){
connection.disconnect();
}
if(XMPP.getInstance().isConnected()){
XMPP.getInstance().close();
}
final String user = AppSettings.getUser(MyService.this);
final String pass = AppSettings.getPassword(MyService.this);
final String username = AppSettings.getUserName(MyService.this);
try {
XMPP.getInstance().login(user, pass, username);
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (PurplKiteXMPPConnectException e) {
e.printStackTrace();
}
}
public void onCreate() {
super.onCreate();
PurplkiteLogs.logDebug(TAG, "in onCreate of Service");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
this.xmppConnection = XMPP.getInstance();
holdConnectionHandler.sendEmptyMessage(0);
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context paramAnonymousContext,
Intent paramAnonymousIntent) {
MyService.this.onLoggedIn();
}
}, new IntentFilter("chatapp.loggedin"));
if ((AppSettings.getUser(this) != null)
&& (AppSettings.getPassword(this) != null)) {
final String user = AppSettings.getUser(this);
final String pass = AppSettings.getPassword(this);
final String username = AppSettings
.getUserName(MyService.this);
new Thread(new Runnable() {
public void run() {
try {
XMPPTCPConnection connection= MyService.this.xmppConnection.getConnection(MyService.this);
if(connection == null || !connection.isAuthenticated()){
MyService.this.xmppConnection.login(user, pass,
username);
}
} catch (XMPPException e) {
PurplkiteLogs.logError(TAG, "Error in Login");
e.printStackTrace();
}
catch (SmackException e) {
PurplkiteLogs.logError(TAG, "Error in Login");
e.printStackTrace();
} catch (IOException e) {
PurplkiteLogs.logError(TAG, "Error in Login");
e.printStackTrace();
} catch (InterruptedException e) {
PurplkiteLogs.logError(TAG, "Error in Login");
e.printStackTrace();
} catch (PurplKiteXMPPConnectException e) {
PurplkiteLogs.logError(TAG, "Error in Login");
e.printStackTrace();
}
MyService.this.sendBroadcast(new Intent(
"chatapp.loggedin"));
return;
}
}).start();
}
}
public class LocalBinder extends Binder {
public LocalBinder() {
}
public MyService getService() {
return MyService.this;
}
}
}
Благодаря