Create and connect totally asynchronously on start of the BeemService.
This fix crash when the service is restart by Android.
--- 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() {