Jon Reading your post just made my day - I never quite understood why Android is so extremely opposed to using non GUI classes for processing - every second operation you make requires a view or an activity. This allows me to update launch simple toasts, and do other stuff where I don't need to know which Activity is currently active (if any at all) from my service without having to go through all the complex constructs that I created (I bind to the service from the Activities / Fragments that need it, then wire up a bunch of event handlers, and unwire them if the Activity/Fragment gets paused again - background processing will proceed in the service, but any and all of the things that need to go on in the GUI get suspended as soon as the service is unbound).
As it almost seems to good to be true, I cannot help but wonder - am I risking anything using the approach you outlined? Regards Stephan Jonathan Pryor-2 wrote > > On Feb 20, 2012, at 11:50 AM, Chris Tacke wrote: >> I've moved to something a bit simpler that, from initial tests anyways, >> appears to work. Does this seem reasonable though (I think it assumes >> that the UIInvoker class itself is created on the UI thread): >> >> public class UIInvoker : DisposableBase >> { >> private Activity m_activity = new Activity(); >> >> public void Invoke(Delegate method) >> { >> m_activity.RunOnUiThread(delegate >> { >> method.DynamicInvoke(null); >> }); >> } > >>I wouldn't suggest this solution, as this avoids a great deal of Activity initialization; you have a "half-baked" Activity instance. (For those reading along at home, Activity.attach() is never invoked; Activity.attach() is called in android.app.ActivityThread, which requires that you go through the Context.startActivity() infrastructure, which `new Activity()` obviously doesn't.) > > That said, it works...because Activity.runOnUiThread() doesn't use > Activity.mUiThread for anything except reference comparison, but I have no > idea how long that will be the case. > > What I would instead suggest is: > > public void Invoke(Delegate method) > { > using (var handler = new > Android.OS.Handler(Android.OS.Looper.MainLooper)) > handler.Post(delegate { > method.DynamicInvoker(null); > }); > } > > - Jon > > _______________________________________________ > Monodroid mailing list > Monodroid@.ximian > > UNSUBSCRIBE INFORMATION: > http://lists.ximian.com/mailman/listinfo/monodroid > -- View this message in context: http://mono-for-android.1047100.n5.nabble.com/how-to-access-an-activity-from-broadcast-receiver-tp5497414p5711744.html Sent from the Mono for Android mailing list archive at Nabble.com. _______________________________________________ Monodroid mailing list Monodroid@lists.ximian.com UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid