Fix 'android.view.ViewRootImpl: Only the original thread that created a view hierarchy can touch its views.' in SubmitPostService.

This commit is contained in:
Alex Ning 2021-06-23 20:05:46 +08:00
parent b01c9b9e2b
commit c7f9343d00

View File

@ -196,14 +196,14 @@ public class SubmitPostService extends Service {
isNSFW, kind, new SubmitPost.SubmitPostListener() { isNSFW, kind, new SubmitPost.SubmitPostListener() {
@Override @Override
public void submitSuccessful(Post post) { public void submitSuccessful(Post post) {
EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null)); handler.post(() -> EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null)));
stopService(); stopService();
} }
@Override @Override
public void submitFailed(@Nullable String errorMessage) { public void submitFailed(@Nullable String errorMessage) {
EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(false, null, errorMessage)); handler.post(() -> EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(false, null, errorMessage)));
stopService(); stopService();
} }
@ -216,14 +216,14 @@ public class SubmitPostService extends Service {
content, flair, isSpoiler, isNSFW, APIUtils.KIND_CROSSPOST, new SubmitPost.SubmitPostListener() { content, flair, isSpoiler, isNSFW, APIUtils.KIND_CROSSPOST, new SubmitPost.SubmitPostListener() {
@Override @Override
public void submitSuccessful(Post post) { public void submitSuccessful(Post post) {
EventBus.getDefault().post(new SubmitCrosspostEvent(true, post, null)); handler.post(() -> EventBus.getDefault().post(new SubmitCrosspostEvent(true, post, null)));
stopService(); stopService();
} }
@Override @Override
public void submitFailed(@Nullable String errorMessage) { public void submitFailed(@Nullable String errorMessage) {
EventBus.getDefault().post(new SubmitCrosspostEvent(false, null, errorMessage)); handler.post(() -> EventBus.getDefault().post(new SubmitCrosspostEvent(false, null, errorMessage)));
stopService(); stopService();
} }
@ -238,22 +238,24 @@ public class SubmitPostService extends Service {
new SubmitPost.SubmitPostListener() { new SubmitPost.SubmitPostListener() {
@Override @Override
public void submitSuccessful(Post post) { public void submitSuccessful(Post post) {
EventBus.getDefault().post(new SubmitImagePostEvent(true, null)); handler.post(() -> {
Toast.makeText(SubmitPostService.this, R.string.image_is_processing, Toast.LENGTH_SHORT).show(); EventBus.getDefault().post(new SubmitImagePostEvent(true, null));
Toast.makeText(SubmitPostService.this, R.string.image_is_processing, Toast.LENGTH_SHORT).show();
});
stopService(); stopService();
} }
@Override @Override
public void submitFailed(@Nullable String errorMessage) { public void submitFailed(@Nullable String errorMessage) {
EventBus.getDefault().post(new SubmitImagePostEvent(false, errorMessage)); handler.post(() -> EventBus.getDefault().post(new SubmitImagePostEvent(false, errorMessage)));
stopService(); stopService();
} }
}); });
} catch (ExecutionException | InterruptedException e) { } catch (ExecutionException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
EventBus.getDefault().post(new SubmitImagePostEvent(false, getString(R.string.error_processing_image))); handler.post(() -> EventBus.getDefault().post(new SubmitImagePostEvent(false, getString(R.string.error_processing_image))));
stopService(); stopService();
} }
} }
@ -280,31 +282,34 @@ public class SubmitPostService extends Service {
type, resource, flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() { type, resource, flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() {
@Override @Override
public void submitSuccessful(Post post) { public void submitSuccessful(Post post) {
EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(true, false, null)); handler.post(() -> {
if (type.contains("gif")) { EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(true, false, null));
Toast.makeText(SubmitPostService.this, R.string.gif_is_processing, Toast.LENGTH_SHORT).show(); if (type.contains("gif")) {
} else { Toast.makeText(SubmitPostService.this, R.string.gif_is_processing, Toast.LENGTH_SHORT).show();
Toast.makeText(SubmitPostService.this, R.string.video_is_processing, Toast.LENGTH_SHORT).show(); } else {
} Toast.makeText(SubmitPostService.this, R.string.video_is_processing, Toast.LENGTH_SHORT).show();
}
});
stopService(); stopService();
} }
@Override @Override
public void submitFailed(@Nullable String errorMessage) { public void submitFailed(@Nullable String errorMessage) {
EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, false, errorMessage)); handler.post(() -> EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, false, errorMessage)));
stopService(); stopService();
} }
}); });
} else { } else {
EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, true, null)); handler.post(() -> EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, true, null)));
stopService(); stopService();
} }
} catch (IOException | InterruptedException | ExecutionException e) { } catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace(); e.printStackTrace();
EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, true, null)); handler.post(() -> EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, true, null)));
stopService(); stopService();
} }