获取Android中接口的所有实现的列表

我正在用Android开发一个项目,想知道每个活动中的Internet连接状态,而我的应用程序有15个以上的活动,因此,我不想在每个活动中实现和初始化networkstatechanged类和接口。 因此,我在扩展Application的类中创建了它的实例,在其中我创建了接口的对象。

我的NetworkStateChanged类:

NetworkConnected.java

package time.real.identify.location.tracker.gps.saksham.com.emptrackerhost;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;

interface NetworkListener {
    void isConnected();
    void isDisconnected();
}


public class NetworkConnected {

    private List<NetworkListener> listeners = new ArrayList<NetworkListener>();
    private boolean isConnected = false;
    private boolean wasConnected = false;

    NetworkConnected() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                isConnected = isOnline();
                wasConnected = isConnected;
                notifySomethingHappened();
                while(true){
                    isConnected = isOnline();
                    if(wasConnected != isConnected){
                        wasConnected = isConnected;
                        notifySomethingHappened();
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    private boolean isOnline() {
        try {
            int timeoutMs = 3000;
            Socket sock = new Socket();
            SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

            sock.connect(sockaddr, timeoutMs);
            sock.close();

            return true;
        } catch (IOException e) {
            return false;
        }
    }
    void addListener(NetworkListener listener) { listeners.add(listener); }
    private void notifySomethingHappened() {
        for (NetworkListener listener : listeners) {
            if(isConnected){
                listener.isConnected();
            }
            else{
                listener.isDisconnected();
            }
        }

    }

}

ApplicationClass.java

package time.real.identify.location.tracker.gps.saksham.com.emptrackerhost;

import android.app.Application;

public class ApplicationClass extends Application {

    public static boolean isNetworkConnected = false;
    @Override
    public void onCreate() {
        super.onCreate();
        new NetworkConnected().addListener(new time.real.identify.location.tracker.gps.saksham.com.emptrackerhost.NetworkListener() {
            @Override
            public void isConnected() {
                isNetworkConnected = true;
            }

            @Override
            public void isDisconnected() {
                isNetworkConnected = false;
            }
        });
    }

}

之后,我只想在每个类中实现一个接口,由于一个对象已经在运行,因此无需将其添加为侦听器或对其进行初始化。

我想要这样的东西:

public class LoginPageClass extends Activity implements NetworkListener
{
public void isConnected(){
//Make a Snackbar
}
public void isDisconnected(){
//Make a Snackbar
}
}

注意:-我不想从ApplicationClass调用myNetworkListener类的对象来添加侦听器(this);

我只想获取实现我的NetworkConnected类的类的列表,以便为它们提供状态更改回调。 可能吗?如果是这样,怎么办?

评论