Last active
June 23, 2022 12:57
-
-
Save esabook/d35dfd3ecb50c15446777a686fedf71f to your computer and use it in GitHub Desktop.
Sample of network state listener, connect or disconnect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ***.services; | |
import android.app.Activity; | |
import android.app.Application; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
/** | |
* monitoring nerworking state, are connected to internet or not. | |
* this class will send event to listener when stateChanged | |
* or when current activity lifecycle is onResume | |
*/ | |
public final class NetworkStateMonitoringService extends BroadcastReceiver | |
implements Application.ActivityLifecycleCallbacks { | |
public static final String TAG = NetworkStateMonitoringService.class.getSimpleName(); | |
private static ConnectionSateListener mConnectionSateListener; | |
private static NetworkStateMonitoringService mInstance; | |
private static IntentFilter mConnectivityIntentFilter; | |
private static boolean isConnected; | |
private static Activity mCurrentActivity; | |
/** | |
* assign this service to application context | |
* | |
* @param ctx app context | |
* @param listener | |
* @return | |
*/ | |
public static NetworkStateMonitoringService with(Application ctx, ConnectionSateListener listener) { | |
mInstance = new NetworkStateMonitoringService(); | |
mConnectivityIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); | |
mConnectionSateListener = listener; | |
try { | |
ConnectivityManager cm = | |
(ConnectivityManager) ctx | |
.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo netInfo = cm.getActiveNetworkInfo(); | |
isConnected = netInfo != null && netInfo.isConnected(); | |
} catch (Exception ignore) { | |
} | |
ctx.registerActivityLifecycleCallbacks(mInstance); | |
return mInstance; | |
} | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if (intent == null) return; | |
isConnected = !intent.getBooleanExtra( | |
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); | |
dispatchListener(mCurrentActivity); | |
} | |
void dispatchListener(Activity activity) { | |
mCurrentActivity = activity; | |
if (mConnectionSateListener != null) | |
mConnectionSateListener.onConnectionStateChanged(activity, isConnected); | |
} | |
/** | |
* | |
*/ | |
public interface ConnectionSateListener { | |
void onConnectionStateChanged(@Nullable Activity activity, boolean isConnected); | |
} | |
//region Application.ActivityLifecycleCallbacks | |
@Override | |
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) { | |
activity.registerReceiver(this, mConnectivityIntentFilter); | |
} | |
@Override | |
public void onActivityStarted(@NonNull Activity activity) { | |
} | |
@Override | |
public void onActivityResumed(@NonNull Activity activity) { | |
dispatchListener(activity); | |
} | |
@Override | |
public void onActivityPaused(@NonNull Activity activity) { | |
} | |
@Override | |
public void onActivityStopped(@NonNull Activity activity) { | |
} | |
@Override | |
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) { | |
} | |
@Override | |
public void onActivityDestroyed(@NonNull Activity activity) { | |
activity.unregisterReceiver(this); | |
} | |
//endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
equivalent in kotlin