简体   繁体   English

如何接口onReceive方法

[英]How to Interface onReceive Method

I've set up a class called NetworkStatus that monitors the network status of the device. 我已经建立了一个名为NetworkStatus的类,该类监视设备的网络状态。 Within NetworkStatus I've defined a BroadcastReceiver that monitors if there has been a connectivity change, ie, if the internet has been switched off or on . NetworkStatus我定义了一个BroadcastReceiver ,它监视连接性是否发生了变化,即,互联网是关闭还是打开 I want to 'expose' this method (ie onReceive ) to the activity that instantiated the instance of NetworkStatus . 我想将此方法(即onReceive )“公开”给实例化NetworkStatus实例的活动。

I'm trying to achieve this by setting up an interface in the NetworkStatus class, but I'm not sure how to call the interface from the onReceive method of the BroadcastReceiver ie, 我试图通过在NetworkStatus类中设置一个interface来实现此目的,但是我不确定如何从BroadcastReceiveronReceive方法调用该接口,即,

public class NetworkStatus {
    public static interface NetworkStatusCallbacks {
        void onReceive();
    }

    public static class ConnectivityChange extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {

            // need to call NetworkStatusCallbacks onReceive here
        }
    }

}

Then in the main activity I would do something like, 然后在主要活动中,我会做类似的事情,

public class MyActivity extends Activity implements NetworkStatus.NetworkStatusCallbacks {

    @Override
    public void onReceive() {
      // Do stuff here
    }
}

Would really appreciate some pointers. 非常感谢您提供一些指导。 Thanks. 谢谢。

Possible Solution 可能的解决方案

Think I have found a solution. 想我已经找到了解决方案。 As Selvin pointed out, to do this requires the Activity . 正如Selvin指出的那样,要做到这一点就需要Activity I therefore obtained the Activity by casting it from the activity context that is passed to a constructor for the NetworkStatus class, ie, setting a constructor for the class as 因此,我通过从传递给NetworkStatus类的构造函数的活动上下文进行强制转换来获得Activity ,即,将该类的构造函数设置为

private static nsc NetworkStatusCallbacks;
public NetworkStatus (Context context) {
    Activity activity = (Activity) context;
    nsc = (NetworkStatusCallbacks) activity;
}

I can then call the interface from the onReceive method of the BroadcastReceiver as follows: 然后,我可以从BroadcastReceiveronReceive方法调用该接口,如下所示:

public void onReceive(Context context, Intent intent) {
    nsc.onReceive();
}

Try something like this: 尝试这样的事情:

    public class NetworkStatus {
        public static interface NetworkStatusCallbacks {
            void onReceive();
        }

        private final NetworkStatusCallbacks m_cb;

        NetworkStatus(NetworkStatusCallbacks cb)
        {
          m_cb=cb;
        }

        public static class ConnectivityChange extends BroadcastReceiver {

            public void onReceive(Context context, Intent intent) {

                // need to call NetworkStatusCallbacks onReceive here
               m_cb.onReceive()
            }
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个方法中调用onReceive方法? - How to call onReceive method in another method? 接口对象在广播 onReceive 方法中具有空值 - Interface Object has null value in Broadcast onReceive method 如何使用BroadcastReceiver中的onReceive方法更新TextView - How to update TextView with onReceive method from BroadcastReceiver 如何在窗口小部件提供程序的onReceive方法中更新TextView - How to update TextView in onReceive method in widget provider Android:如何等待IntentService for BroadcastReceiver onReceive方法 - Android: How to wait IntentService for BroadcastReceiver onReceive method 如何删除在onReceive方法上捕获的相同短信 - How to delete same sms that was captured on onReceive method 如何从BroadcastReceiver的onReceive方法开始活动? - How to start activity from BroadcastReceiver onReceive method? 如何取消覆盖BroadcastReceiever中的onReceive方法 - How to un-override the onReceive method in BroadcastReceiever 如何在BroadCastReceiver的onReceive方法内的TextView中设置Text? - How to setText in TextView inside onReceive method of BroadCastReceiver? 如何在Android中的BroadcastReceiver onReceive方法内调用另一个方法? - How to call another method inside BroadcastReceiver onReceive method in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM