在一个服务的BroadcastReceiver不能接收广播的Andr​​oid [英] BroadcastReceiver within a Service not receiving broadcasts Android

查看:103
本文介绍了在一个服务的BroadcastReceiver不能接收广播的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了这个程序,用户在其中一个活动更新某些变量,并使用一个BroadcastReceiver这个活动传递新的变量到IntentService。然而,在IntentService所述的BroadcastReceiver似乎没有被接收到的广播。这是服务中的code创建广播接收器

 保护类UpdateReceiver扩展的BroadcastReceiver {
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        Log.d(接收器,得到的消息:);
        //状态= 3;
        //Toast.makeText(context,知道了,Toast.LENGTH_SHORT).show();
    }
};
 

和这里的code注册接收器,在onHandle()函数

 的IntentFilter的IntentFilter =新的IntentFilter();
intentFilter.addAction(更新);
UpdateReceiver笑=新UpdateReceiver();
DetectService.this.registerReceiver(笑,IntentFilter的);
 

最后,这里的code发送广播,从活动

 意图broadcastIntent =新意图();
broadcastIntent.setAction(更新);
HomePageActivity.this.sendBroadcast(broadcastIntent);
Log.d(发件人,送味精);
 

当我把接收器在相同的活性广播的一部分,它的工作原理,但不是当我把它改成了IntentService。请帮助!

在另外一个相关的说明,我已经在这个项目中使用LocalBroadcastManager因为广播是所有地方,但月食似乎并不能够导入兼容性一流尝试。我使用的是Android SDK管理器已安装了它。是否有任何事情,我做错了什么?

解决方案
  

该活动通过使用一个BroadcastReceiver新的变量到IntentService。

这是没有意义的。使用 startService()将命令发送到 IntentService 。和 IntentService 不应该有一个的BroadcastReceiver ,因为 IntentService 将尽快销毁 onHandleIntent()完成,因此将永远不会收到该广播。

  

我在这个项目中使用LocalBroadcastManager因为广播是所有地方,但月食似乎并不能够导入兼容性一流尝试。

::耸肩::

下面是一个示例项目与Eclipse项目文件使用 LocalBroadcastManager 。创建项目时我遇到没有特别Eclipse的问题。

I've got this app, in which users update certain variables in an Activity, and this Activity passes the new variables to a IntentService using a BroadcastReceiver. However, the BroadcastReceiver in the IntentService doesn't seem to be receiving the broadcasts. This is the code within the service to create the broadcast receiver

protected class UpdateReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent){
        Log.d("receiver", "Got message: ");
        //state=3;
        //Toast.makeText(context, "got it", Toast.LENGTH_SHORT).show();
    }
};

And here's the code to register the receiver, in the onHandle() function

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("updates");
UpdateReceiver lol = new UpdateReceiver();
DetectService.this.registerReceiver(lol, intentFilter);

Finally here's the code to send the broadcast, from the activity

Intent broadcastIntent = new Intent();
broadcastIntent.setAction("updates");
HomePageActivity.this.sendBroadcast(broadcastIntent);
Log.d("sender", "send msg");

When I put the receiver in the same activity as the broadcasting part, it works, but not when I put it into the IntentService. Help please!

On another related note, I've tried using LocalBroadcastManager in this project since the broadcasts are all local but eclipse doesn't seem to be able to import the compatibility class. I've installed it using Android SDK manager already. Is there any thing I'm doing wrong here?

解决方案

this Activity passes the new variables to a IntentService using a BroadcastReceiver.

That makes no sense. Use startService() to send a command to an IntentService. And an IntentService should not have a BroadcastReceiver, because the IntentService will be destroyed as soon as onHandleIntent() completes and therefore will never receive the broadcast.

I've tried using LocalBroadcastManager in this project since the broadcasts are all local but eclipse doesn't seem to be able to import the compatibility class.

:: shrug ::

Here is a sample project with Eclipse project files that uses LocalBroadcastManager. I encountered no particular Eclipse issues when creating the project.

这篇关于在一个服务的BroadcastReceiver不能接收广播的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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