Skip to content

Instantly share code, notes, and snippets.

@myohei
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myohei/8974633 to your computer and use it in GitHub Desktop.
Save myohei/8974633 to your computer and use it in GitHub Desktop.
ネットワークの状態変化を取得するReceiver
public class NetworkStateReceiver extends BroadcastReceiver {
public static final IntentFilter NETWORK_INTENT_FILTER = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
private static final String TAG = NetworkStateReceiver.class.getSimpleName();
private static boolean sBeforeNetworkContext = false;
private NetworkStateListener mNetworkStateListener;
public NetworkStateReceiver() {
}
public NetworkStateReceiver(NetworkStateListener listener) {
this.mNetworkStateListener = listener;
}
@Override
public void onReceive(Context context, Intent intent) {
final Bundle extra = intent.getExtras();
if (extra == null) {
return;
}
__Log.d(TAG, "=====================================");
__Log.d(TAG, "EXTRA_EXTRA_INFO: " + extra.getString(ConnectivityManager.EXTRA_EXTRA_INFO));
__Log.d(TAG, "EXTRA_IS_FAILOVER: " + extra.getString(ConnectivityManager.EXTRA_IS_FAILOVER));
__Log.d(TAG, "EXTRA_NETWORK_INFO: " + extra.getString(ConnectivityManager.EXTRA_NETWORK_INFO));
__Log.d(TAG, "EXTRA_NETWORK_TYPE: " + extra.getString(ConnectivityManager.EXTRA_NETWORK_TYPE));
__Log.d(TAG, "EXTRA_NO_CONNECTIVITY: " + extra.getString(ConnectivityManager.EXTRA_NO_CONNECTIVITY));
__Log.d(TAG, "EXTRA_REASON: " + extra.getString(ConnectivityManager.EXTRA_REASON));
__Log.d(TAG, "ConnectionArrive? : " + Utils.arriveConnection(context));
__Log.d(TAG, "=====================================\n");
if(mNetworkStateListener == null) {
return;
}
final boolean nowNetworkContext = Utils.arriveConnection(context);
if (sBeforeNetworkContext == nowNetworkContext) {
return;
}
sBeforeNetworkContext = nowNetworkContext;
if(Utils.arriveConnection(context)){
mNetworkStateListener.onConnect();
} else {
mNetworkStateListener.onDisConnect();
}
}
public interface NetworkStateListener {
public void onConnect();
public void onDisConnect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment