呼叫从一个BroadcastReceiver的活动方式。可能吗?方式、BroadcastReceiver

2023-09-12 04:46:38 作者:゛ 自作多情丶

我坚持开发一个应用程序,因为我需要调用我的活性的方法,当一个BroadcastReceiver被触发。我想要的BroadcastReceiver检测时,网络连接断开,并打电话给我,我已经写活性的方法。

I'm stuck developing an app because I need to call a method of my activity when a BroadcastReceiver is triggered. I want the BroadcastReceiver to detect when a network connection goes down and call a method of my activity which I've already written.

我一直在寻找,我发现越来越多的人都问过这个之前,但是没有人得到了有关如何做到这一点的答案。

I've been searching and I found that more people had asked this before but nobody got an answer about how to do it.

我想,也许Android的API不允许它。如果这是不可能打电话给我的活动从BroadcastReceiver的方法是否有其他方法可以做到这一点?

I think that maybe android's API doesn't allow it. If it's impossible to call a method of my activity from the BroadcastReceiver are there other ways to do this?

感谢。

推荐答案

尝试是这样的。

在你活动code写的

BroadcastReceiver connectionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            myActivityMethod();// Here you call method
        }
    };

    registerReceiver(connectionReceiver, new IntentFilter("com.test.NET_CONNECTION"));

和在服务写入

Intent intent = new Intent("com.test.NET_CONNECTION");
        sendBroadcast(intent);

如果任何混乱让我知道我会尽力解决。

If any confusion let me know i'll try to solve..