[app] Fix toast on worker thread (#120)

This commit is contained in:
tehcneko 2021-02-12 15:56:08 +08:00 committed by GitHub
parent d9362b921e
commit 83e6182252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -4,6 +4,8 @@ import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
@ -24,6 +26,7 @@ public class App extends Application {
private static OkHttpClient okHttpClient;
private static Cache okHttpCache;
private SharedPreferences pref;
private final Handler handler = new Handler(Looper.getMainLooper());
public static App getInstance() {
return instance;
@ -87,4 +90,8 @@ public class App extends Application {
}
return okHttpCache;
}
public void runOnUiThread(Runnable runnable) {
handler.post(runnable);
}
}

View File

@ -78,7 +78,7 @@ public class RepoLoader {
.build()).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG).show();
App.getInstance().runOnUiThread(() -> Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG).show());
synchronized (this) {
isLoading = false;
}