简体   繁体   English

适用于Android的Java中的performSelectorOnMainThread等效项

[英]performSelectorOnMainThread equivalent in Java for Android

I have an iPhone application and I am porting it over to Android. 我有一个iPhone应用程序,正在将其移植到Android。

I have a service/controller that spawns new threads to perform some network tasks. 我有一个服务/控制器,它会产生新的线程来执行某些网络任务。

I use performSelectorOnMainThread in my iPhone app. 我在iPhone应用程序中使用performSelectorOnMainThread How can I have my Java app do the same or similar thing? 如何让我的Java应用执行相同或相似的操作?

Options: 选项:

  1. manual threading + Handler or runOnUiThread(..) available from Activity for posting UI changes 手动线程+ 处理程序runOnUiThread(..)可从Activity获得,用于发布UI更改
  2. AsyncTask 的AsyncTask
  3. Service for lengthy background operations 服务于冗长的后台操作

Assuming performSelectorOnMainThread does what I think it does, look at runOnUiThread() in Activity , or post() on Handler or any of your Views (ie, your GUI widgets). 假设performSelectorOnMainThread做了我想做的事情,请查看Activity中的runOnUiThread()Handler或任何Views (即您的GUI窗口小部件)上的post() )。

Though I generally agree with alex's answer -- use AsyncTask where possible, rather than forking your own threads. 尽管我通常同意alex的回答-尽可能使用AsyncTask ,而不要分叉自己的线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM