안드로이드의 백그라운드 스레드를 사용하여 진행률 막대를 업데이트하는 방법은 무엇입니까? 진행률 표시 줄의 진행률도 변경됩니다. 도와주세요.스레드를 사용하여 진행률 막대를 업데이트하는 방법
AndroidVogue
안드로이드의 백그라운드 스레드를 사용하여 진행률 막대를 업데이트하는 방법은 무엇입니까? 진행률 표시 줄의 진행률도 변경됩니다. 도와주세요.스레드를 사용하여 진행률 막대를 업데이트하는 방법
AndroidVogue
은 내가 AsyncTask
를 사용하여 유사한 작업을 수행했다. AsyncTask
에는 onProgressUpdate(Integer)
메서드가 있습니다. 예를 들어 doInBackground()
중에 진행이 완료 될 때마다 publishProgress()
을 호출하여 각 반복을 호출 할 수 있습니다.
자세한 내용은 docs을 참조하십시오. 당신이해야 할
사용 핸들러이 핸들러가 진행 표시 줄을 업데이트 할 것은 : 1) 핸들러
Thread t=new Thread()
{
public void run()
{
while(some condition)
{
sleep(1000);
Message myMessage = new Message();
myMessage.obj = "success";
handler.sendMessage(myMessage);
}
}
};
private Handler handler=new Handler()
{
@Override
public void handleMessage(Message msg)
{
String result=(String)msg.obj;
if(result.equals("success")
{
//update progress bar here
}
}
};
private ProgressBar pgb;
private TextView textview;
private int progressBarStatus = 0;
private Handler progressBarbHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
pgb = (ProgressBar) findViewById(R.id.progressBar2) ;
}
public void load_bar(View view)
{
pgb.setMax(100);
pgb.setProgress(0);
new Thread(new Runnable() {
@Override
public void run() {
for (progressBarStatus = 0; progressBarStatus <= 100; progressBarStatus++) {
progressBarbHandler.post(new Runnable() {
public void run() {
pgb.setProgress(progressBarStatus);
}
});
try {
Thread.sleep(50);
} catch (Exception ex) {
}
}
}
}).start();
}
코드를 설명해 주시겠습니까? – thanksd
// this is demonstrate how to update progress bar from a thread
private ProgressBar pgb; // is a progressBar named with pgb
private int progressBarStatus = 0;// is a variable named progressBarStatus and initialize by 0
// in onCreate method find the ui component view horizontal prograssbar named progressBar2
public void load_bar(View view) // is a button OnClick
// and i am using a thread with for loop to demonstrate a task and variable progressBarStatus as status
pgb = (ProgressBar) findViewById(R.id.progressBar2) ;// find view by id
pgb.setMax(100); // at this point we the maximum value of a progress bar to make it full like we make 100 pices of it
pgb.setProgress(0); // initialize the progress bar with 0 % progress
// progress bar handler that update the status of progress bar . without handler we cant update progress bar from thread
progressBarbHandler.post(new Runnable() {
public void run() {
pgb.setProgress(progressBarStatus);
}
에서 진행 표시 줄이 2 스레드에서) 핸들러를 업데이트 할 메시지를 보내
웹에서 JSON 개체를 다운로드하는 동안 진행률 막대를 업데이트하는 방법에 대한 아이디어. 그것은 거의 20 초 걸립니다. 시간이 고정되어 있지 않습니다. 그것은 더 적을 것이다. 진행률 표시 줄의 진행률을 늘리는 방법은 무엇입니까? –
@AndroidVogue : 코드 샘플을 제공 할 수 있습니까? 그래서 당신을 도울 수 있습니다 –
스레드에서 할 수있는 또 하나의 작업은'yourActivity.this.runOnUiThread (new Runnable() {public void run() {/ * 진행률 막대를 여기에서 업데이트하십시오} * /}})' –