1. 쓰레드로 처리
class MainActivity
package com.example.choi.mystudy23;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv;
Button btn01 , button02;
ProgressDialog dialog=null;
boolean cancelFlag=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv_01);
btn01=(Button)findViewById(R.id.bt_01);
btn01.setOnClickListener(listener);
button02=(Button)findViewById(R.id.button02);
button02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
}
View.OnClickListener listener=new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_01:
cancelFlag=true;
MyThread myThread =new MyThread(MainActivity.this, 100);
myThread.setDaemon(true);
myThread.start();
break;
}
}
};
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what ==1){
// tv.setText(String.valueOf(num));
dialog.setProgress(msg.arg1);
}
}
};
class MyThread extends Thread{
Context context;
private int param;
int num=0;
public MyThread(Context context, int param){
this.context=context;
this.param=param;
myDialog();
}
@Override
public void run() {
while (cancelFlag){
num++;
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(String.valueOf(num));
}
});
if(num ==param){
dialog.dismiss();
cancelFlag=false;
}
Message msg =Message.obtain();
msg.what=1;
msg.arg1=num;
handler.sendMessage(msg);
try{
Thread.sleep(100);
}catch (Exception e){
e.printStackTrace();
}
}
}
public void myDialog(){
dialog=new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setTitle("Dialog");
dialog.setMessage("쓰레드 사용");
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
cancelFlag=false;
num=0;
tv.setText("Cancelled");
}
});
dialog.setProgress(0);
dialog.show();
}
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.mystudy23.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_01"
android:textSize="30sp"
android:textStyle="normal|bold|italic"
android:textAlignment="center"
android:textColor="@android:color/holo_red_dark" />
<Button
android:text="시작"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:id="@+id/bt_01"
android:background="@color/colorAccent"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@android:color/background_light" />
<Button
android:text="AsyncTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_01"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:id="@+id/button02" />
</RelativeLayout>
2.AsnycTask
class Main2Activity
package com.example.choi.mystudy23;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
TextView tv;
Button btn01;
NewAsyncTask newAsyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
tv=(TextView)findViewById(R.id.tv_01);
btn01=(Button)findViewById(R.id.bt_01);
btn01.setOnClickListener(listener);
}
View.OnClickListener listener=new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_01:
newAsyncTask=new NewAsyncTask(Main2Activity.this, tv);
newAsyncTask.execute(100, 50, 100);
break;
}
}
};
}
class NewAsyncTask
package com.example.choi.mystudy23;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
/**
* Created by choi on 2017-03-09.
*/
public class NewAsyncTask extends AsyncTask<Integer, String , Integer> {
final static String TAG="NewAsyncTask";
Context mContext=null;
TextView mTv=null;
ProgressDialog dialog=null;
boolean cancelFlag =false;
public NewAsyncTask(Context context, TextView textView){
this.mContext=context;
this.mTv=textView;
}
//UI 작업 전 실행 - Dialog 띄우기
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute()");
dialog=new ProgressDialog(mContext);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setTitle("Dialog");
dialog.setMessage("AsyncTask");
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CanCel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
cancelFlag=true;
//onCancelled() 호출
cancel(true);
}
});
dialog.setProgress(0);
dialog.show();
}
//프로그램 계산
@Override
protected Integer doInBackground(Integer... params) {
Log.i(TAG, "doInBackground()");
Log.i(TAG, "params[0] : " +params[0]);
Log.i(TAG, "params[1] : "+params[1]);
//parmas[0] 값 100 을 가지고서
for(int i=0; i<params[0]; i++){
//onProgressUpdate() 호출
publishProgress(String.valueOf(i));
try{Thread.sleep(200); }catch (Exception e){ e.printStackTrace();}
if(cancelFlag)break;
}
return params[0];
}
//UI 처리 부분
@Override
protected void onProgressUpdate(String... values) {
Log.i(TAG, "onProgressUpdate()");
int i=Integer.parseInt(values[0]);
dialog.setProgress(i);
mTv.setText(String.valueOf(i));
}
//작업 후 종료 ex) dialog 종료
@Override
protected void onPostExecute(Integer integer) {
Log.i(TAG, "onPostExecute()");
Log.i(TAG, "result :" + integer);
mTv.setText(String.valueOf(integer));
dialog.dismiss();
}
//취소시 발생
@Override
protected void onCancelled(Integer integer) {
Log.i(TAG, "onCancelled()");
mTv.setText("Cancelled");
dialog.dismiss();
}
}
R.layout.main2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.mystudy23.Main2Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_01"
android:textSize="30sp"
android:textStyle="normal|bold|italic"
android:textAlignment="center"
android:textColor="@android:color/holo_red_dark" />
<Button
android:text="시작"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:id="@+id/bt_01"
android:background="@android:color/holo_blue_dark"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@android:color/background_light" />
</RelativeLayout>
댓글 ( 4)
댓글 남기기