Create and connect totally asynchronously on start of the BeemService.
authorDa Risk <da_risk@beem-project.com>
Sun, 23 Sep 2012 00:49:33 +0200
changeset 987 dd0ddb657924
parent 986 2c85a0f62ab0
child 988 586e34fc7594
Create and connect totally asynchronously on start of the BeemService. This fix crash when the service is restart by Android.
src/com/beem/project/beem/BeemService.java
--- a/src/com/beem/project/beem/BeemService.java	Sat Sep 22 03:21:58 2012 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Sun Sep 23 00:49:33 2012 +0200
@@ -260,11 +260,7 @@
     public void onStart(Intent intent, int startId) {
 	super.onStart(intent, startId);
 	Log.d(TAG, "onStart");
-	try {
-	    mConnection.connectAsync();
-	} catch (RemoteException e) {
-	    e.printStackTrace();
-	}
+	createConnectAsync();
     }
 
     /**
@@ -347,6 +343,34 @@
     }
 
     /**
+     * Utility method to create and make a connection asynchronously.
+     */
+    private synchronized void createConnectAsync() {
+	if (mConnection == null ) {
+	    new Thread(new Runnable() {
+
+		@Override
+		public void run() {
+		    createConnection();
+		    connectAsync();
+		}
+	    }).start();
+	} else
+	    connectAsync();
+    }
+
+    /**
+     * Utility method to connect asynchronously.
+     */
+    private void connectAsync() {
+	try {
+	    mConnection.connectAsync();
+	} catch (RemoteException e) {
+	    Log.w(TAG, "unable to connect", e);
+	}
+    }
+
+    /**
      * Install the MemorizingTrustManager in the ConnectionConfiguration of Smack.
      */
     private void initMemorizingTrustManager() {