AsyncTask örneği Android
AsyncTask hakkında okuyordum, ve basit bir program altında çalıştım. Ama çalışmak için görünmüyor. Nasıl yapabilirim?
package com.test;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.System;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class AsyncTaskActivity extends Activity {
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener((OnClickListener) this);
}
public void onClick(View view){
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
for(int i=0;i<5;i ) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
TextView txt = (TextView) findViewById(R.id.output);
txt.setText("Executed");
return null;
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
Sadece arka plan işlemi 5 saniye sonra etiketini değiştirmek için çalışıyorum.
Bu benim main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@ id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="10"
android:padding="10dip">
</ProgressBar>
<Button
android:id="@ id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Progress" >
</Button>
<TextView android:id="@ id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Replace"/>
</LinearLayout>
CEVAP
Tamam, başka bir iş parçacığı aracılığıyla GUI ulaşmaya çalışıyorsunuz. Bu, esas olarak, iyi bir uygulama değil.
Bu AsyncTask görüşlerinizi nerede GUI erişimi olan başka bir iş parçacığı, doInBackground()
içindeki her şeyi yürütür.
preExecute()
postExecute()
teklif erişim için GUI öncesi ve sonrası ağır kaldırmak oluşur bu yeni bir konu, hatta geçmesi sonucu uzun çalışma için postExecute()
göster herhangi bir sonuç işlenmesi.
Daha sonra TextView güncelleme nerede bu satırları
TextView txt = (TextView) findViewById(R.id.output);
txt.setText("Executed");
PostExecute()
koydum
Sonra TextView metin doInBackground
tamamlandıktan sonra güncelleme göreceksiniz.
DÜZENLEME:Özelliği dinleyiciniz kontrol seçildiğini görmek için etmediğini fark ettim. Yapmak için en kolay yolu bu ifadeler geçiş yoluyla buluyorum. Tam bir sınıf aşağıdaki tüm önerileri ile düzenlenmiş karışıklık kurtarmak zorundayım.
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.System;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class AsyncTaskActivity extends Activity implements OnClickListener {
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
// because we implement OnClickListener we only have to pass "this"
// (much easier)
btn.setOnClickListener(this);
}
public void onClick(View view) {
// detect the view that was "clicked"
switch (view.getId()) {
case R.id.button1:
new LongOperation().execute("");
break;
}
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i ) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.output);
txt.setText("Executed"); // txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
Uzun süren İşlemler için Android Async...
android.destek.v7.iç.widget.ActionBarO...
Çalışmıyor AsyncTask doİnBackground An...
Android: AsyncTask vs Servisi...
Android AsyncTask Testi ile test Çerçe...