Android Open Source - mcdroid Network Watcher






From Project

Back to project page mcdroid.

License

The source code is released under:

Apache License

If you think the Android project mcdroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cn.mibcxb.android.util;
//from  www .  j  a v a 2s . c  o m
import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkWatcher {
    private static NetworkWatcher instance;

    public static NetworkWatcher createInstance(Context context) {
        if (instance == null) {
            instance = new NetworkWatcher(context);
        }
        return instance;
    }

    public static NetworkWatcher getInstance() {
        return instance;
    }

    public static void destroyInstance(Context context) {
        if (instance != null) {
            instance.destroy(context);
            instance = null;
        }
    }

    private final BroadcastReceiver receiver;
    private final List<NetworkListener> list;
    private NetworkInfo info;

    private NetworkWatcher(Context context) {
        if (context == null) {
            throw new IllegalArgumentException();
        }

        list = new ArrayList<NetworkListener>();
        receiver = new NetworkStateReceiver();
        context.registerReceiver(receiver, new IntentFilter(
                ConnectivityManager.CONNECTIVITY_ACTION));
    }

    private void destroy(Context context) {
        if (context == null) {
            throw new IllegalArgumentException();
        }
        context.unregisterReceiver(receiver);
    }

    public boolean registerListener(NetworkListener listener) {
        if (listener != null && !list.contains(listener)) {
            listener.onNetworkChanged(info);
            return list.add(listener);
        }
        return false;
    }

    public boolean unregisterListener(NetworkListener listener) {
        return list.remove(listener);
    }

    class NetworkStateReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent
                    .getAction())) {
                ConnectivityManager manager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);

                info = manager.getActiveNetworkInfo();
                for (NetworkListener listener : list) {
                    listener.onNetworkChanged(info);
                }
            }
        }
    }

    public interface NetworkListener {
        void onNetworkChanged(NetworkInfo info);
    }
}




Java Source Code List

cn.mibcxb.acra.CrashReportWriter.java
cn.mibcxb.android.app.McApplication.java
cn.mibcxb.android.os.McHandler.java
cn.mibcxb.android.util.Logger.java
cn.mibcxb.android.util.NetworkWatcher.java
cn.mibcxb.util.McIO.java
cn.mibcxb.util.McMath.java