广播接收器方面与对话 [英] BroadcastReceiver context with dialog

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

问题描述

我想实现以下


  1. 一个活动启动一个后台任务(SyncAdapter),并显示一个对话框。

  2. 后台任务发送广播。

  3. 这些广播应该由对话框中截获。

什么我希望是这样的:

 对话广播
  < -------------------
 好!
  < -------------------
 好!         ....  < -------------------
 好!

不过,我所得到的是这样的:

 对话广播
  < -------------------
  < -------------------         ....  < -------------------    (对话解雇) 好!
 好!         ....
 好!

如上图所示,广播被正确发送,但在对话框关闭后才可被接收。我希望收到他们正在所示的对话框时。换句话说,步骤1和2都工作正常,但三是工作部分。我想可能是问题是,我没有使用的广播接收器的适当的上下文。

所以我的问题是:我应该使用哪种情况下认购的更新(即接收广播,不给他们),使他们能够通过对话被拾起正在显示时,

我试着 Activity.this Activity.this.getBaseContext() Activity.this.getApplicationContext() dialog.getContext() DialogFragment.getActivity()无济于事。

只是为了澄清:我用的BroadcastReceiver模式多次,用它对话时,旁边我只是遇到了麻烦。


解决方案

事实:


  • 活动(A)显​​示对话框(D)

  • 未知类型(财产以后)命名的后台任务(T)

  • (T)将广播到广播接收器(R)

  • (R)应该解雇(D)

问题:


  • (T)你的意思是类从AbstractThreadedSyncAdapter出来的?

  • 请您somhow的(T)启动和显示(D)后完成(A)?

结论:


  • 不管,如果你的(A)你做什么创建和显示(D)是负责解雇(D)及做befor中的onStop得到()状态

  • 换句话说,如果活动将不被解雇对话框死了,你会得到


  android.view.WindowLeaked会抛出异常。



  • &安培;当您试图操纵对话框中,您将获得:


  java.lang.IllegalArgumentException异常:查看没有连接到窗口管理器


第二部分你的问题,它涉及的背景


  • 您随时可以检查哪些方面是通过方法分配给对话框


  Dialog.getContext()



  • USEN它来搭配其他的上下文


  Dialog.getContext()等于(上下文)。



  • 也ü可以使用类方法


  Class.isAssignableFrom(类<> C)



  • 如果u想匹配上下文类(u使用例如活动)


  MyActivity.class.isAssignableFrom(ObjectToMatch.getClass());


和我会忘记补充:

您可以随时开始对话框中使用APLICCATION背景:)

&安培;还有一件事:

在对案件UI线程完成涉及UI建议立即进行删除的任何东西,你会忘了&安培;获得


  android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。


编辑:

我不看不出什么问题:工作液

I'm trying to achieve the following

  1. An Activity starts a background task (SyncAdapter) and shows a dialog.
  2. The background task sends broadcasts.
  3. These broadcasts should be intercepted by the dialog.

What I would expect is this:

Dialog           Broadcast
  <-------------------
 OK!
  <-------------------
 OK!

         ....

  <-------------------
 OK!

But what I get is this:

Dialog           Broadcast
  <-------------------
  <-------------------

         ....

  <-------------------

    (dialog dismiss)

 OK!
 OK!

         ....
 OK!

As the diagram shows, the broadcasts are being sent correctly, but they are being received only after the dialog is dismissed. I want to receive them when the dialog is being shown. In other words, steps 1 and 2 are working fine, but 3. is working partially. What I think might be the problem is that I'm not using the proper context for the BroadcastReceiver.

So my question is: Which context should I use to subscribe for updates (i.e. receive the broadcasts, not send them), so that they can be picked up by the dialog when it is being shown?

I've tried with Activity.this, Activity.this.getBaseContext() and Activity.this.getApplicationContext(), dialog.getContext(), DialogFragment.getActivity() to no avail.

Just to clarify: I've used the BroadcastReceiver pattern multiple times, I'm just having trouble when using it alongside a dialog.

解决方案

FACTS:

  • ACTIVITY (A) shows DIALOG (D)
  • unkown type of (SOMTHING) named BACKGROUND TASK (T)
  • (T) SENDS BROADCAST TO BROADCASTRECEIVER (R)
  • (R) SHOULD DISMISS (D)

QUESTIONS:

  • (T) do you mean CLASS DERIVED from AbstractThreadedSyncAdapter?
  • do you somhow finish (A) after start of (T) and show (D)?

CONCLUSION:

  • regardless what you do if your (A) create and shows (D) it is responsible to dismiss (D) & to do it befor gets in onStop() state
  • other words if activity will die without dismissed dialog you will get

android.view.WindowLeaked exception will be thrown.

  • & when you try manipulate the dialog you will get:

java.lang.IllegalArgumentException: View not attached to window manager

The second part of your question, which concerns context

  • you always can check which context is assigned to Dialog by method

Dialog.getContext()

  • usen it to match other context

Dialog.getContext().equals(Context);

  • also u can use Class method

Class.isAssignableFrom(Class<?> c)

  • if u wanna match context to class (for example Activity u use)

MyActivity.class.isAssignableFrom(ObjectToMatch.getClass());

and I would forget to add:

you can always START DIALOG USING APLICCATION CONTEXT :)

& one more thing:

any stuff involving UI shoul by done on UI THREAD in case you will forgot & get

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

edit:

I DONT SEE ANY PROBLEM : WORKING SOLUTION

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

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