728x90
반응형
package com.gitsn.android.prism.Utilities;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
public abstract class GS_AsyncTask <T1,T2> {
/*
* 사용 예시
* new GS_AsyncTask<String,String>(){
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String arg) {
return null;
}
@Override
protected void onPostExecute(String result) {
}
}.execute("TEST");
* */
T1 mArgument; // Argument
T2 mResult; // Result
// Execute
final public void execute(final T1 arg) {
// Store the argument
mArgument = arg;
// Call onPreExecute
onPreExecute();
// do Background
Observable.fromCallable(()->{
return doInBackground(arg);
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<T2>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull T2 t2) {
mResult = t2;
}
@Override
public void onError(@NonNull Throwable e) {
// Call onPostExecute
onPostExecute(null);
}
@Override
public void onComplete() {
// Call onPostExecute
onPostExecute(mResult);
}
});
}
// onPreExecute
protected abstract void onPreExecute();
// doInBackground
protected abstract T2 doInBackground(T1 arg);
// onPostExecute
protected abstract void onPostExecute(T2 result);
}
728x90
반응형
'Android' 카테고리의 다른 글
Android Swtich 스위치 ( 커스텀 ) (0) | 2024.01.11 |
---|---|
Android cutout 영역 ( IOS의 노치 영역 ) (0) | 2023.09.06 |
Android System Font Scale 수정하기 (0) | 2023.08.29 |
Android Edit Text 키보드 Show/Hide Event Listener (0) | 2023.08.29 |
Android 키보드 숨기기 And EditText 포커스 없애기 (1) | 2023.04.19 |