Last active
January 19, 2018 13:15
-
-
Save demixdn/5441026dfdd2eb405123472bbcbc0f32 to your computer and use it in GitHub Desktop.
Простая организация асинхронной работы (Callback + Runnable + Executor)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.util.concurrent.Executor; | |
import java.util.concurrent.Executors; | |
/** | |
* A simple {@link Fragment} subclass. | |
*/ | |
public class BlankFragment extends Fragment implements Callback<String> { | |
private TextView textView; | |
public BlankFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
textView = new TextView(getActivity()); | |
textView.setText(R.string.hello_blank_fragment); | |
return textView; | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
Executor executor = Executors.newSingleThreadExecutor(); | |
executor.execute(new MyHardWork(this, "very important param")); | |
} | |
@Override | |
public void onSuccess(String result) { | |
textView.setText(result); | |
} | |
@Override | |
public void onError(Exception ex) { | |
Log.e(getClass().getSimpleName(), "onError: " + ex.getMessage(), ex); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Callback<T> { | |
void onSuccess(T result); | |
void onError(Exception ex); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Handler; | |
import android.os.Looper; | |
import android.support.annotation.NonNull; | |
import java.lang.ref.WeakReference; | |
public class MyHardWork implements Runnable { | |
private final WeakReference<Callback<String>> callbackRef; | |
private final String myVeryImportantParam; | |
private final Handler mainHandler = new Handler(Looper.getMainLooper()); | |
public MyHardWork(@NonNull Callback<String> callback, @NonNull String myVeryImportantParam) { | |
this.callbackRef = new WeakReference<>(callback); | |
this.myVeryImportantParam = myVeryImportantParam; | |
} | |
@Override | |
public void run() { | |
try { | |
Thread.sleep(5000); | |
//do a very important work here | |
final String result = "Created at " + Thread.currentThread().getName() + " thread with " + myVeryImportantParam; | |
//then return the result to the callback | |
mainHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
Callback<String> callback = callbackRef.get(); | |
if (callback != null) { | |
callback.onSuccess(result); | |
} | |
} | |
}); | |
} catch (final Exception e) { | |
mainHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
Callback<String> callback = callbackRef.get(); | |
if (callback != null) { | |
callback.onError(e); | |
} | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment