Skip to content Skip to sidebar Skip to footer

Broadcastreceiver Context With Dialog

I'm trying to achieve the following An Activity starts a background task (SyncAdapter) and shows a dialog. The background task sends broadcasts. These broadcasts should be interce

Solution 1:

FACTS:

  • ACTIVITY (A) shows DIALOG (D)
  • unknown type of (SOMETHING) 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 somehow 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 before 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: Viewnot attached towindow 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 APPLICATION CONTEXT :)

& one more thing:

any stuff involving UI should 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 DON'T SEE ANY PROBLEM : WORKING SOLUTION

THREAD CALLING RECEIVER

Solution 2:

This is a BroadcastReceiver and you adapt it on your code. Instead of Activity.this, Activity.this.getBaseContext() and Activity.this.getApplicationContext() you should use Context like this :

classBroadCastReceiverTestextendsBroadcastReceiver {
Context context;
publicvoidonReceive(Context c, Intent intent) {
    this.context = c;

}

Now you have to use this.context as a Context.

Solution 3:

Try Otto

https://github.com/square/otto

From documentation,

An enhanced Guava-based event bus with emphasis on Android support.

Otto is an event bus designed to decouple different parts of your application while still allowing them to communicate efficiently.

Forked from Guava, Otto adds unique functionality to an already refined event bus as well as specializing it to the Android platform.

Post a Comment for "Broadcastreceiver Context With Dialog"