앱이 동작하면서 어떤 백그라운드 처리를 하려고 할 때 UI를 블럭해야하는 경우가 있다.
이 때 백그라우드 처리가 중복되거나 복수의 처리 다른 Activity에서 각각 프로그래스 처리를 하다 보면 중복된 프로그래스로 인하여 사용자가 혼란스럽거나, 매끄러운 처리가 안될 수 있다.
이럴 때 어플리케이션이 한개의 프로그래스 다이얼로그를 공유하여 처리하면 깔끔하게 UI를 블럭할 수 있다.
1. 프로그래스 클래스
public class LoadingProgress {
public static Activity activity;
private static LoadingProgress loadingProgress;
private ProgressDialog progressDialog = null;
private Handler timeoutHandler = new Handler();
private CustomAlertDialog mesgbox;
public static Handler resultHandler;
private Runnable timeoutCheckRunnable = new Runnable() {
@Override
public void run() {
mesgbox = Common.alertMessage(activity,
activity.getResources().getString(R.string.app_name),
activity.getResources().getString(R.string.progress_not_response),
activity.getResources().getString(R.string.btn_ok),
new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (progressDialog != null) {
progressDialog.dismiss();
}
if(timeoutHandler != null) {
timeoutHandler.removeCallbacks(null);
}
mesgbox = null;
if(resultHandler!=null) {
Message msg2 = new Message();
msg2.what = Constant.RESPONSE_TIMEOUT;
resultHandler.sendMessage(msg2);
}
}
});
if (mesgbox != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mesgbox.create();
}
mesgbox.show();
}
}
};
public LoadingProgress() {
}
public static synchronized LoadingProgress getInstance(Activity act) {
activity = act;
if(loadingProgress==null) {
loadingProgress = new LoadingProgress();
}
return loadingProgress;
}
public static synchronized LoadingProgress getInstance(Activity act, Handler handler) {
activity = act;
resultHandler = handler;
if(loadingProgress==null) {
loadingProgress = new LoadingProgress();
}
return loadingProgress;
}
public void show() {
if(progressDialog != null && progressDialog.isShowing()) {
return;
}
progressDialog = new ProgressDialog(activity);
progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
progressDialog.show();
progressDialog.setContentView(R.layout.custom_progressdialog_process);
TextView mesg = (TextView) progressDialog.findViewById(R.id.cpp_progress_message);
mesg.setText(activity.getResources().getString(R.string.server_loading_message));
progressDialog.setCancelable(false);
timeoutHandler.postDelayed(timeoutCheckRunnable, (Constant.CONNECT_TIMEOUT));
}
public void dismiss() {
try {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (timeoutHandler != null && timeoutCheckRunnable != null) {
timeoutHandler.removeCallbacks(timeoutCheckRunnable);
}
} catch(Exception e){
e.printStackTrace();
}
}
public boolean isShow() {
if(progressDialog != null && progressDialog.isShowing()) return true;
else return false;
}
}
2. ProgressDialog layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/progressdialog"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/cpp_progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/cpp_progress_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="@string/progress_default_message"
android:textColor="@color/White"
android:textStyle="bold"
android:visibility="gone" />
</LinearLayout>
3. 기타 선언된 값들
public class Constant {
public final static int CONNECT_TIMEOUT = 35000;
public final static int READ_TIMEOUT = 35000;
public static final int RESPONSE_SUCCESS = 0;
public static final int RESPONSE_FAILURE = 1;
public static final int RESPONSE_TIMEOUT = 2;
.
.
.
}
4. strings.xml
<string name="progress_not_response">응답이 없습니다.</string>
5. 사용법
public class MainActivity extends AppCompatActivity {
LoadingProgress loadingProgress;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
..
loadingProgress = LoadingProgress.getInstance(this);
..
}
private void setPhotoUI() {
loadingProgress.show();
if(valetDriverInfoResponse!=null) {
getPhotos(valetDriverInfoResponse.getValetData().getResponseId(), PhotoRequest.ORDER_TYPE_PARKED);
}
loadingProgress.dismiss();
}
..
..
}
6. 마지막으로 주의할 점은 어플리케이션당 하나의 인스탄스만 존재하기 때문에 show와 dismiss를 하는 순간 처리되게 되는게 장점인 반면
의도하지 않게 dismiss가 되는 경우가 발생할 수 있습니다.
프로그래스의 사용 프로세스는 보다 투명하고 명쾌하게 사용하고, 난발해서는 안됩니다.
감사합니다.
'개발관련 정보 > 안드로이드' 카테고리의 다른 글
번역키보드 소개 (0) | 2021.03.10 |
---|---|
테스트용 런처 함께만들기 (0) | 2021.02.24 |
메소드 버스 활용하기 (0) | 2019.04.22 |
5.0 롤리팝에서 숫자 콤마 표현 (0) | 2015.04.22 |
TextView에 HTML TAG 보여주기 (0) | 2015.01.23 |