Skip to content

Instantly share code, notes, and snippets.

@demixdn
Last active January 19, 2018 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demixdn/5441026dfdd2eb405123472bbcbc0f32 to your computer and use it in GitHub Desktop.
Save demixdn/5441026dfdd2eb405123472bbcbc0f32 to your computer and use it in GitHub Desktop.
Простая организация асинхронной работы (Callback + Runnable + Executor)
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);
}
}
public interface Callback<T> {
void onSuccess(T result);
void onError(Exception ex);
}
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