# HG changeset patch # User Da Risk # Date 1348354173 -7200 # Node ID dd0ddb6579240c9b2e63831653f309234753b45b # Parent 2c85a0f62ab0fbae8c4dcd6dc542bf5e5b6498b3 Create and connect totally asynchronously on start of the BeemService. This fix crash when the service is restart by Android. diff -r 2c85a0f62ab0 -r dd0ddb657924 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() {