广播接收器和ResultReceiver在安卓 [英] Broadcast Receiver and ResultReceiver in android

查看:228
本文介绍了广播接收器和ResultReceiver在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别的的BroadcastReceiver ResultReceiver 在Android的?

解决方案

结果接收器:

 通用接口,用于从别人接受回调的结果。
 

广播接收器:

 基类code,将接收sendBroadcast发送意图()。
 

编辑:

背景:所有网络操作/长期运行的操作应在远离主线程。两种方法可以做到这一点:

  1. 在异步任务 - 对于简单的网络好比说retreive图像/做数据库 加工
  2. 服务 - 对于复杂的长期运行的后台进程

如果您需要在主线程之外执行的工作,但只有在用户与应用程序交互,那么你或许应该改为创建一个新的线程,而不是服务。例如,如果你要播放一些音乐,但是只有当您的活动正在运行,您可以创建一个异步线程。但是,如果你想这个过程继续在用户退出应用程序(比如下载),然后使用一个服务,即使

假设你挑2.现在

  1. 您的活动发送Web请求为您服务
  2. 您的服务执行,使用说DefaultHttpClient
  3. 它发回的数据为您的活动。

    这里接收数据的第三步可以通过两种方式来完成

  

1)广播接收器:多个接收器接收数据。使用,如果你想在应用程序之间传送数据/通知(说你也与FB和Twitter的互动,多个接收器为你的网络广播),   只要发送广播其已发送系统。

     

2)结果接收器:您的应用程序中的数据的唯一接收者。这是你实现并将其传递给通过putExtra的intentService接口。 IntentService然后将获取该对象   并调用其receiver.send函数发送任何东西(的包)到   调用活动。结果接收机   preference通过广播接收器,如果你的所有的通信是   内置到应用程序

编辑:我还要提到的这种谨慎

  

注意:一个服务运行在其主办的主线流程的   服务不创建其自己的线程,并不在单独运行   流程(除非另行指定)。这意味着,如果   服务是打算做任何CPU密集型工作或阻塞操作   (如MP3播放或网络),你应该创建一个新的线程   在服务中做到这一点的工作。通过使用一个单独的线程,则   会降低应用程序的风险不响应(ANR)错误和   应用程序的主线程可以继续致力于用户交互   与你的活动。

What is difference between BroadcastReceiver and ResultReceiver in android?

解决方案

Result Receiver:

Generic interface for receiving a callback result from someone.

Broadcast Receiver:

Base class for code that will receive intents sent by sendBroadcast().

EDIT:

Background: All networking operations/long running operations should take place away from the main thread. Two ways to do this :

  1. Async task - For Simple networking like say retreive an image/ do db processing
  2. Service - For Complex long running background process

If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create an Async Thread. But if you want the process to continue even after the user exits the app (say a download) then use a service

Lets say you pick 2. Now

  1. You activity sends a web request to your service
  2. Your service executes that using say DefaultHttpClient
  3. It sends back data to your activity.

    The third step of receiving data here can be done in two ways

1.) Broadcast receiver: Multiple receivers can receive your data. Used if you want to send data/notifications across applications(say you are also interacting with fb and twitter, multiple receivers for your web broadcast), whenever you send broadcast its sent system wide.

2.) Result receiver: Your application is the only receiver of the data. It is an Interface you implement and pass it to the intentService through putExtra. IntentService will then fetch this object and call its receiver.send function to send anything (in bundle) to calling activity. Result receiver has preference over broadcast receivers if your all communication is internal to your application

EDIT: I should also mention this caution

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

这篇关于广播接收器和ResultReceiver在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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