마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

AsyncTask 질문 있습니다.

–4 추천

 

지금 제가 프로그래스 바를 하나 만들었거든요. 사용자가 MaxValue, IncrementalValue 지정해서 프로그래스바가 작동하는 방법인데요

여기서 이것을 AsyncTask 이용해서 바꾸려고하는데 어떻게해야하는지 도저히 모르겠네요. 

도와주실분....?

 


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener{

	ProgressDialog dialog;
	int increment;
	int maximum;	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		Button btn1 = (Button)findViewById(R.id.button1);
		btn1.setOnClickListener(this);
	    Button btn2 = (Button) findViewById(R.id.button2);
		//btn2.setOnClickListener(this);

		
		
	}
	
	public void onClick(View v){
		EditText et = (EditText)findViewById(R.id.incrementValue);
		increment = Integer.parseInt(et.getText().toString());
		
		
		dialog = new ProgressDialog(this);
        dialog.setCancelable(true);
        dialog.setMessage("Loading...");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);
         
        EditText max = (EditText) findViewById(R.id.maximumValue);
        maximum = Integer.parseInt(max.getText().toString());
        
        dialog.setMax(maximum);
        dialog.show();
        
        Thread background = new Thread (new Runnable() {
            public void run() {
                try {
                  while (dialog.getProgress()<= dialog.getMax()) {
                        Thread.sleep(500);
  
                        progressHandler.sendMessage(progressHandler.obtainMessage());
                    }
                } catch (java.lang.InterruptedException e) {
                }
            }
         });
          
         background.start();
  
     }
      
     Handler progressHandler = new Handler() {
         public void handleMessage(Message msg) {
             dialog.incrementProgressBy(increment);
         }
     };
	/*	
     btn2.setOnClickListener(new OnClickListener(){
    	
			@Override
			public void onClick2(View v) {
				startActivity(new Intent (this, ListActivityMainActivity.class)) ;
			}
		});
*/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
}

 

익명사용자 님이 2013년 8월 27일 질문

1개의 답변

0 추천

AsyncTask<Void, Void, Void> mTask = null;
mTask = new AsyncTask<Void, Void, Void>() {
 @Override
 protected void onPreExecute() {
  super.onPreExecute();
 }

 @Override
 protected Void doInBackground(Void... params) {
  return null;
 }

 @Override
 protected void onPostExecute(Void result) {
  mTask = null;
 }

 @Override
 protected void onCancelled() {
  super.onCancelled();
 }
};
mTask.execute(null, null, null);

 loop 관련부분은 doInBackground 넣으시면 되구요.

onPreExecute() 는 시작전 초기화 구문 넣어주시고

onPostExecute() 는 종료 후 처리할 구문 넣으시고

onCancelled() 는 loop 비정상 종료 시 처리할 구문 넣으시면 됩니다~

익명사용자 님이 2013년 8월 28일 답변
...