服务绑定到的BroadcastReceiver [英] Bind Service to BroadcastReceiver

查看:119
本文介绍了服务绑定到的BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些的服务类,注册多个报警。

I have some Service class, which registers multiple alarms.

在我的的BroadcastReceiver 类,我想在的onReceive ()方法调用的服务的一些方法类。

In my BroadcastReceiver class, I want the onReceive() method to call some method of the Service class.

不过,我不看我怎么可以绑定在一起。我试图使的BroadcastReceiver 一个内部类,但后来我得到了更多的错误,也不会触发报警的。

However, I don't see how I can bind them together. I tried to make the BroadcastReceiver an inner class, but then I got more errors and couldn't fire the alarm at all.

感谢

推荐答案

看<一href="http://developer.android.com/reference/android/content/BroadcastReceiver.html">http://developer.android.com/reference/android/content/BroadcastReceiver.html生命周期。的BroadcastReceiver仅用于处理消息创建的。这意味着,它的寿命很短,蚂蚁也是无状态的。所以,你不能绑定任何东西给它。

Look at http://developer.android.com/reference/android/content/BroadcastReceiver.html life cycle. BroadcastReceiver is created only for handling a message. It means that it's life is very short, ant it is also stateless. So you cannot bind anything to it.

反正你可以尝试启动的服务的形式的onReceive(上下文的背景下,意图意图)的BroadcastReceiver的的方法, 像这样的:

Anyway you can try to start a service form onReceive(Context context, Intent intent) method of BroadcastReceiver, like this:

    public void onReceive(Context context, Intent intent) {
        Intent intent2 = new Intent(context, GCMService.class);
        intent2.putExtras(intent);
        context.startService(intent2);
}

则a =服务应处理广播消息。

Then a=the service should handle a broadcast message.

这篇关于服务绑定到的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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