Need to unregister for network intent receiver on shutdown to prevent leaks.

This commit is contained in:
Bryce Curtis 2011-05-31 15:13:54 -05:00
parent 10e1808c56
commit 39ec9c095d

View File

@ -57,13 +57,15 @@ public class NetworkManager extends Plugin {
private static final String LOG_TAG = "NetworkManager"; private static final String LOG_TAG = "NetworkManager";
private String connectionCallbackId; private String connectionCallbackId;
ConnectivityManager sockMan; ConnectivityManager sockMan;
TelephonyManager telephonyManager; TelephonyManager telephonyManager;
BroadcastReceiver receiver;
/** /**
* Constructor. * Constructor.
*/ */
public NetworkManager() { public NetworkManager() {
this.receiver = null;
} }
/** /**
@ -76,18 +78,23 @@ public class NetworkManager extends Plugin {
super.setContext(ctx); super.setContext(ctx);
this.sockMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); this.sockMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
this.telephonyManager = ((TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE)); this.telephonyManager = ((TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE));
this.connectionCallbackId = null;
// We need to listen to connectivity events to update navigator.connection // We need to listen to connectivity events to update navigator.connection
IntentFilter intentFilter = new IntentFilter() ; IntentFilter intentFilter = new IntentFilter() ;
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
ctx.registerReceiver(new BroadcastReceiver() { if (this.receiver == null) {
@Override this.receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) { @Override
updateConnectionInfo((NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO)); public void onReceive(Context context, Intent intent) {
} updateConnectionInfo((NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO));
}, intentFilter); }
} };
ctx.registerReceiver(this.receiver, intentFilter);
}
}
/** /**
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
* *
@ -135,6 +142,19 @@ public class NetworkManager extends Plugin {
// All methods take a while, so always use async // All methods take a while, so always use async
return false; return false;
} }
/**
* Stop network receiver.
*/
public void onDestroy() {
if (this.receiver != null) {
try {
this.ctx.unregisterReceiver(this.receiver);
} catch (Exception e) {
System.out.println("Error unregistering network receiver: " + e.getMessage());
}
}
}
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// LOCAL METHODS // LOCAL METHODS