在Android中运行新线程和UI线程的方法是什么?

据我所知,当我们运行进程时,他们会在主线程中启动它们。 当我们做一些较重的工作时,我们使用一个新线程。 如果我们想修改UI外观,我们使用run on UI。

有人可以向我解释这些线程做了什么以及如何使用它们?

UI线程和主线程只是同一线程的不同名称。

应用程序的所有UI通胀都在此主线程上完成。 我们将“更重”的工作委托给其他线程的原因是因为我们不希望这些操作减慢UI的响应性和通胀时间。

您将需要运行任何更改UI或修改主线程上UI使用的对象的操作。

AsyncTask的一个例子

package com.wolfdev.warriormail; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; public class LoginActivity extends Activity implements OnClickListener{ private Button loginButton; private EditText eText; private EditText pText; private CheckBox box; private String user; private String pass; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.login); //Initialize UI objects on main thread loginButton = (Button) findViewById(R.id.button1); loginButton.setOnClickListener(this); eText = (EditText) findViewById(R.id.editText1); pText = (EditText) findViewById(R.id.editText2); eText.clearFocus(); pText.clearFocus(); Animation fadeIn = AnimationUtils.loadAnimation(this,R.anim.fadeanimation); Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slideanimation); eText.startAnimation(slideIn); pText.startAnimation(slideIn); box = (CheckBox)findViewById(R.id.checkBox1); box.startAnimation(fadeIn); login.startAnimation(fadeIn); } @Override public void onClick(View v) { user = email.getText().toString(); password = pass.getText().toString(); } class LoginTask extends AsyncTask{ @Override protected Void doInBackground(Void... args){ /* Here is where you would do a heavy operation * In this case, I want to validate a users * credentials. If I would do this on the main * thread, it would freeze the UI. Also since * this is networking, I am forced to do this on * a different thread. */ return null; } @Override protected void onPostExecute(Void result){ /* This function actually runs on the main * thread, so here I notify the user if the * login was successful or if it failed. If * you want update the UI while in the background * or from another thread completely, you need to * use a handler. */ } } } 

我将研究以下两件事:

处理程序和AsyncTask

对于Android线程来说,这是一个非常好的资源。 http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

此外,如果你问,因为你将获取一些数据/进行简单的API调用,我肯定会建议你查看http://loopj.com/android-async-http/ 。 这将使您的生活更加简单。

ThreadUI Thread 。 因此,当您启动Activity您将进入Main (UI) Thread 。 如果您想使用单独的线程来执行“繁重的工作”,例如网络进程,那么您有几个选择。 您可以在Activity创建一个单独的Thread并调用runOnUiThread来更新UI 。 您还可以使用AsyncTask进行短期操作。 根据文档,事情可能只需要几秒钟。 这是一个简短的例子:

 public class TalkToServer extends AsyncTask { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); } @Override protected String doInBackground(String... params) { //do your work here return something; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); // do something with data here-display it or send to mainactivity 

你会从中调用它

 TalkToServer myAsync = new TalkToServer() //can add params if you have a constructor myAsync.execute() //can pass params here for `doInBackground()` method 

只要确保不要尝试在doInBackground()方法中更新UI 。 使用任何其他或将数据传递回Activity方法。 如果您的AsyncTask类是Activity固有方法,那么您可以使用其context来更新UI 。 如果它在自己的文件中,那么你需要将context传递给它的构造函数

 TalkToServer myAsync = new TalkToServer(this); 

您也可以阅读本文

无痛线程