Android如何从其他类更新(UI线程)(真的吗?) [英] Android how to update (UI thread) from other classes (really?)

查看:162
本文介绍了Android如何从其他类更新(UI线程)(真的吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能了解 Google云消息传递

问题是,当服务器触发gcm消息时,我的应用程序会从google play服务收到一个包,这发生在 GcmBroadcastReceiver.java 。在这里,我可以将这些数据发送到其他类,以便从服务器追加一些信息。当我尝试更新时,例如在UI线程中出现了一些视图时,我被卡住了。

The problem is that when a gcm message triggers by the server, my application receives a bundle from google play services, this happen at GcmBroadcastReceiver.java. Here i can send this data to other classes in order to append some info from the server.. well. I got stuck when i try to update, for example, some views in the UI thread.

如何操作?

想象一下,当我声明视图等时, MainActivity.java 是UI线程。

Imagine that MainActivity.java is the UI thread when i declare the views, etc.

我试图在这里创建一个公共静态方法,可以通过这种方式直接调用 GcmBroadcastReceiver.java MainActivity。* updateUI *(args .. ),但它抛出这个异常:

I tried to create here a public static method which can be called directly by GcmBroadcastReceiver.java by this way: MainActivity.*updateUI*(args..), but it throws this exception:

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

任何人都可以试着解释一下吗?我也知道关于asyncTask,但我无法想象它是如何工作的。我还发现一些页面解释了由UI线程触发的事件,它就像在后台执行一些任务的runnables一样。我搜索这样的东西:

Can anyone try to explain me this? i also know about asyncTask but i cant imagine how it works. I also find some pages explaining events that are fired by the UI thread it self like runnables that do some task in background. Im searching something like this:

MainActivity extends Activity{

    ...
    protected void onCreate(Bundle blabla)..{

    setContentView(R.layout.blabla);

    registerSomeEvent(this);

    }

    private void handleEvent(Bundle ...){

    ... do stuff with the data provided in the UI thread

    }

} 

这里 GcmBroadcastReceiver ,当gcm推送一些数据时,触发该神奇事件,以便在UI线程中执行更新,并使用 ListViews TextView

And here at GcmBroadcastReceiver, when gcm push some data, trigger that magic event in order to perform updates at the UI thread with some views like ListViews or TextView

推荐答案

一种方法是使用use LocalBroacastManager 。对于如何实现,在如何使用LocalBroadcastManager?上有一个很好的例子。

One way is to use use LocalBroacastManager. For how to implement is, there is a great example on how to use LocalBroadcastManager?.


LocalBroadcast Manager是注册并向您的流程中的本地对象发送Intents广播的助手。您广播的数据不会离开您的应用,因此不必担心泄露私人数据。

LocalBroadcast Manager is a helper to register for and send broadcasts of Intents to local objects within your process. The data you are broadcasting won't leave your app, so don't need to worry about leaking private data.`

您的活动可以注册这个本地广播。从 GCMBroadcastReceiver 中,当您收到GcmBroadcastReceiver中的内容时,您将发送本地广播。在您的活动内,您可以收听广播。这样,如果活动处于最前沿/活跃状态,它将接收广播,否则它不会。因此,无论何时您收到本地广播,您都可以在活动开放的情况下执行所需的操作。这就好像对活动说的那样:嘿活动,我收到了一条消息,做任何你想要的东西。

Your activity can register for this local broadcast. From the GCMBroadcastReceiver, you send a local broadcast when you receive something in GcmBroadcastReceiver. Inside your Activity you can listen to the broadcast. This way if the activity is in the forefront/is active, it will receive the broadcast otherwise it won't. So, whenever you receive that local broadcast, you may do the desired action if activity is open. This is like saying to the activity that "Hey Activity, I've received a message. Do whatever you want with it".

如果你想为整体做应用程序,那么你可以让所有的活动扩展一个抽象的活动。在这个抽象活动类中,你可以注册这个'LocalBroadcast'。其他方法是在你的所有活动中注册LocalBroadcast(但是你必须管理你将只显示一次消息的方式)。

If you want to do for the whole app, then you can make all your activities extend an abstract activity. And inside this abstract activity class you can register it for this 'LocalBroadcast'. Other way is to register for LocalBroadcast inside all your activities (but then you'll have to manage how you'll show the message only once).

这篇关于Android如何从其他类更新(UI线程)(真的吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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