gpt4 book ai didi

android - 如何在 Asmack 中处理临时异常

转载 作者:行者123 更新时间:2023-11-30 02:36:17 25 4
gpt4 key购买 nike

我建立了一个基于 aSmack 的应用程序,显然所有关于连接的东西都在一个服务中运行,所以保持它的连接是非常重要的,因为我们知道在后台运行的服务可能会在手机资源不足时被杀死(通常是 Ram),因此在服务上使用 START_STICKY 标志时,它会以空 Intent 重新启动。

现在我想知道是不是手机上没有网络,或者那个时候突然发生了意外的临时异常,(并且因为服务重新启动了reconnectionManager还没有设置),所以必须重新启动应用程序才能检索其连接.我的问题是如何处理这些异常?我知道我可以做这样的事情:

 public void connect(){
try {
connection.connect();
} catch (SmackException | IOException | XMPPException e) {

if(getIntent() == null){
connect();
return;
}
}
}

但这是不专业的 imo,我知道有一种方法可以确定临时异常,但不幸的是我无法记住或找到它们。任何信息表示赞赏。非常感谢

最佳答案

这就是我所做的,工作完美,每件事都算在内。

这是我的服务每次启动时所做的事情

private void connect(){

if (!connection.isConnected()){


try {
connection.connect();
} catch (SmackException | IOException | XMPPException e) {

mainHandler.post(new Runnable() {

@Override
public void run() {
if(intent ==null){
Toast.makeText(getBaseContext(),"Could not Connect to The Server , Network Problems , Retrying in 30 Seconds...", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getBaseContext(),"Could not Connect to The Server , Network Problems...", Toast.LENGTH_LONG).show();
}

}
});

if(intent == null){
//When intent is null, It Means that service got Destroyed middle of app, which
//means user has already connected and Authenticated once, but can not do it again.
//so thats the key

nonMainHandler.postDelayed(new Runnable() {

@Override
public void run() {
connect();

}
},30000);
}


e.printStackTrace();
}


}

try {
if (connection.isConnected() && !connection.isAuthenticated()) {


try {
connection.login(LMApplication.userName,LMApplication.passWord);
} catch (SmackException | IOException | XMPPException e) {

mainHandler.post(new Runnable() {

@Override
public void run() {
if(intent != null){
Toast.makeText(getBaseContext(),"Could not Login Using this Information..", Toast.LENGTH_LONG).show();
}

}
});

e.printStackTrace();

configEnterButton(-1);

}


}


if(connection.isAuthenticated()){

configEnterButton(100);


try {
Thread.sleep(300);
} catch (InterruptedException e1) {

e1.printStackTrace();
}


goAhead();




Log.i("XMPPChatDemoActivity", "Logged in as" + LMApplication.Raw_CurrentUser);
//TODO
// check if need to set presence from shared preferences
Presence p = new Presence(Presence.Type.available,"", 42, Mode.available);
try {
connection.sendPacket(p);
} catch (NotConnectedException e1) {

e1.printStackTrace();
}


}else if(intent == null){

nonMainHandler.post(new Runnable() {

@Override
public void run() {

if(connection.isConnected() && !connection.isAuthenticated()){

try {
connection.login(LMApplication.userName,LMApplication.passWord);
} catch (XMPPException | SmackException
| IOException e) {

e.printStackTrace();
}
}

if(connection.isConnected() && connection.isAuthenticated()){

notifyReconnect();

}else if(connection.isConnected()){

nonMainHandler.postDelayed(this,10000);

}
}});

}

关于android - 如何在 Asmack 中处理临时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com