呼叫从一个BroadcastReceiver的活动方式。可能吗? [英] Call an activity method from a BroadcastReceiver. Is it possible?

查看:139
本文介绍了呼叫从一个BroadcastReceiver的活动方式。可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持开发一个应用程序,因为我需要调用我的活性的方法,当一个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..

这篇关于呼叫从一个BroadcastReceiver的活动方式。可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆