所以這裏是我所做的,完美的作品,每一件事都在這個數字裏面。
這是awhat我的服務並在每次啓動
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);
}
}});
}
多數民衆贊成還是個菜鳥的答案TBH – Reza 2014-10-21 01:09:20
我不知道回答什麼其他的時間。也許你可以改寫你的問題。 – Flow 2014-10-21 07:55:04
當然,好吧,我會交配 – Reza 2014-10-21 15:47:15