プロセスを実行しているときにAndroidで理解できる限り、プロセスはメインスレッドで開始されます。より重い作業を行うときは、新しいスレッドを使用します。UI の外観を変更したい場合は、run on UI を使用します。
誰かがこれらのスレッドが何をし、どのように使用されているかを説明できますか?
プロセスを実行しているときにAndroidで理解できる限り、プロセスはメインスレッドで開始されます。より重い作業を行うときは、新しいスレッドを使用します。UI の外観を変更したい場合は、run on UI を使用します。
誰かがこれらのスレッドが何をし、どのように使用されているかを説明できますか?
以下の2点について調べてみました。
これは、Android スレッドの非常に優れたリソースです。http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html
また、データを取得したり、単純な API 呼び出しを行ったりするために質問している場合は、http://loopj.com/android-async-http/を確認することをお勧めします。これにより、あなたの人生はずっとシンプルになります。
メインThread
はUI Thread
. だからあなたがあなたを始めるとき、Activity
あなたは上にいMain (UI) Thread
ます。別のスレッドを使用してネットワーク プロセスなどの「重い作業」を行いたい場合は、いくつかのオプションがあります。Thread
内部に別の を作成し、Activity
runOnUiThread を呼び出して を更新することができますUI
。短期間の操作にはAsyncTaskを使用することもできます。ドキュメントによると、数秒しかかからないことがあります。その短い例を次に示します。
public class TalkToServer extends AsyncTask<String, String, String> {
@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
メソッドUI
で更新しようとしないでください。他のいずれかを使用するか、データをメソッドdoInBackground()
に戻します。クラスが の内部メソッドであるActivity
場合、そのクラスを更新に使用できます。独自のファイルにある場合は、次のようにコンストラクターに渡す必要がありますAsyncTask
Activity
context
UI
context
TalkToServer myAsync = new TalkToServer(this);
あなたもこれを読みたいかもしれません
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<Void,Void,Void>{
@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.
*/
}
}
}