@@ -99,21 +99,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
99
99
this .sockMan = (ConnectivityManager ) cordova .getActivity ().getSystemService (Context .CONNECTIVITY_SERVICE );
100
100
this .connectionCallbackContext = null ;
101
101
102
- // We need to listen to connectivity events to update navigator.connection
103
- IntentFilter intentFilter = new IntentFilter ();
104
- intentFilter .addAction (ConnectivityManager .CONNECTIVITY_ACTION );
105
- if (this .receiver == null ) {
106
- this .receiver = new BroadcastReceiver () {
107
- @ Override
108
- public void onReceive (Context context , Intent intent ) {
109
- // (The null check is for the ARM Emulator, please use Intel Emulator for better results)
110
- if (NetworkManager .this .webView != null )
111
- updateConnectionInfo (sockMan .getActiveNetworkInfo ());
112
- }
113
- };
114
- webView .getContext ().registerReceiver (this .receiver , intentFilter );
115
- }
116
-
102
+ this .registerConnectivityActionReceiver ();
117
103
}
118
104
119
105
/**
@@ -147,6 +133,69 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
147
133
* Stop network receiver.
148
134
*/
149
135
public void onDestroy () {
136
+ this .unregisterReceiver ();
137
+ }
138
+
139
+ @ Override
140
+ public void onPause (boolean multitasking ) {
141
+ this .unregisterReceiver ();
142
+ }
143
+
144
+ @ Override
145
+ public void onResume (boolean multitasking ) {
146
+ super .onResume (multitasking );
147
+
148
+ this .unregisterReceiver ();
149
+ this .registerConnectivityActionReceiver ();
150
+ }
151
+
152
+ //--------------------------------------------------------------------------
153
+ // LOCAL METHODS
154
+ //--------------------------------------------------------------------------
155
+
156
+ private void registerConnectivityActionReceiver () {
157
+ // We need to listen to connectivity events to update navigator.connection
158
+ IntentFilter intentFilter = new IntentFilter ();
159
+ intentFilter .addAction (ConnectivityManager .CONNECTIVITY_ACTION );
160
+ if (this .receiver == null ) {
161
+ this .receiver = new BroadcastReceiver () {
162
+ @ Override
163
+ public void onReceive (Context context , Intent intent ) {
164
+ // (The null check is for the ARM Emulator, please use Intel Emulator for better results)
165
+ if (NetworkManager .this .webView != null ) {
166
+ updateConnectionInfo (sockMan .getActiveNetworkInfo ());
167
+ }
168
+
169
+ String connectionType = null ;
170
+ if (NetworkManager .this .lastInfo == null ) {
171
+ connectionType = TYPE_NONE ;
172
+ } else {
173
+ try {
174
+ connectionType = NetworkManager .this .lastInfo .get ("type" ).toString ();
175
+ } catch (JSONException e ) {
176
+ LOG .d (LOG_TAG , e .getLocalizedMessage ());
177
+ connectionType = TYPE_NONE ;
178
+ }
179
+ }
180
+
181
+ if (TYPE_NONE .equals (connectionType )) {
182
+ boolean noConnectivity = intent .getBooleanExtra (ConnectivityManager .EXTRA_NO_CONNECTIVITY , false );
183
+ LOG .d (LOG_TAG , "Intent no connectivity: " + noConnectivity );
184
+ if (noConnectivity ) {
185
+ LOG .d (LOG_TAG , "Really no connectivity" );
186
+ } else {
187
+ LOG .d (LOG_TAG , "!!! Switching to unknown" );
188
+ sendUpdate (TYPE_UNKNOWN );
189
+ }
190
+ }
191
+ }
192
+ };
193
+ }
194
+
195
+ webView .getContext ().registerReceiver (this .receiver , intentFilter );
196
+ }
197
+
198
+ private void unregisterReceiver () {
150
199
if (this .receiver != null ) {
151
200
try {
152
201
webView .getContext ().unregisterReceiver (this .receiver );
@@ -158,10 +207,6 @@ public void onDestroy() {
158
207
}
159
208
}
160
209
161
- //--------------------------------------------------------------------------
162
- // LOCAL METHODS
163
- //--------------------------------------------------------------------------
164
-
165
210
/**
166
211
* Updates the JavaScript side whenever the connection changes
167
212
*
0 commit comments