从非活动对象访问 UI-Thread,例如 iOS 中的 ([self PerformSelectorOnMainThread])
我已经从 iOS 应用程序开发转向 Android,所以在寻找我以前使用过的一些功能时遇到了一些问题。
android中是否有像[self PerformSelectorOnMainThread:@selector() withObject: waitUntilDone:];
这样的函数可以从任何类的实例访问,知道这个类不是继承自Activity类。
我发现有一个函数 runOnUiThread 可以执行此操作,但我无法使用它,因为我需要从非活动对象调用它,那么有没有办法访问当前的可见的活动或者我应该使用其他解决方案。
I've moved from iOS applications development to android so I'm facing some problems finding some functions I used to use before.
Is there a function like [self performSelectorOnMainThread:@selector() withObject: waitUntilDone:];
in android that can be accessed from an instance of any class, knowing that this class is not inheriting from the Activity class.
I've found that there is a function runOnUiThread
that does this, but I couldn't use it since I need to call it from a non-activity object, so is there a way to access the current visible activity or should I use another solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您没有活动对象,则可以使用处理程序。 “Handler”类可以更新UI。句柄提供了接收消息和可运行对象的方法。要使用处理程序,您必须对其进行子类化并重写handleMessage() 来处理消息。要处理可运行对象,您可以使用方法 post();您的活动中只需要一个处理程序实例。
您的线程可以通过方法 sendMessage(Message msg) 或 sendEmptyMessage 发布消息。
示例示例
在此示例中,我们使用“Handler”类来更新后台线程中的 ProgressBar。
使用活动“ProgressTestActivity”创建一个新的 Android 项目“de.vogella.android.handler”。创建以下布局“main.xml”。此布局包含 ProgressBar 并通过样式设置其外观。
将您的活动更改为以下内容:
运行您的应用程序。一旦按下按钮,进度条就会从后台线程更新。
该示例来自 http://www.vogella.de/articles/AndroidPerformance/ Article.html#concurrency_handler
If you don't have an activity object, you can use handlers. The class "Handler" can update the UI. A handle provides methods for receiving messages and for runnables. To use a handler you have to subclass it and override handleMessage() to process messages. To process runnables, you can use the method post(); You only need one instance of a handler in your activity.
You thread can post messages via the method sendMessage(Message msg) or sendEmptyMessage.
Sample example
In this example we use the class "Handler" to update a ProgressBar in a background thread.
Create a new Android project "de.vogella.android.handler" with the activity "ProgressTestActivity". Create the following layout "main.xml". This layout contains the ProgressBar and sets its appearance via a style.
Change your activity to the following:
Run your application. Once you press your button the ProgressBar will get updated from the background thread.
The sample is from http://www.vogella.de/articles/AndroidPerformance/article.html#concurrency_handler
任何类都可以访问
Activity
,只要您在该类中公开一些可以获取当前Activity
或Context
的公共方法。例如:这没有什么魔力。 Java 与 Objective-C 一样,是一种面向对象的语言。问题是,为了“赋予”普通的旧 java 对象当前的 Activity,您必须从该 Activity 中将其传递给它。
Any class can access the
Activity
so long as you expose some public method in that class that can grab hold of the currentActivity
orContext
. For example:There's no magic to it. Java, like Objective-C is an object-oriented language. The thing is that in order to "give" your plain old java object the current
Activity
, you'll have to pass it to it from within thatActivity
.