异步调用的回调函数是在子线程当中的,需要用Handler或者runOnUiThread来更新UI。
package com.xl.travelassistant.utils;
import android.app.Activity;
import android.util.Log;
import android.widget.Toast;
public class ToastUtil {
public static void showToast(final Activity activity, final String message) {
if ("main".equals(Thread.currentThread().getName())) {
Log.e("ToastUtil", "在主线程");
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
} else {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e("ToastUtil", "不在主线程");
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
}
});
}
}
}
调用:
ToastUtil.showToast(LoginActivity.this, msg);