在activity 中添加
private static Toast toast=null;
private static TextView textView = null;
private boolean isShowToast=false;
onCreate初始化
toast = new Toast(this);
textView = new TextView(this);
textView.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(textView);
定义 public 方法供线程调用
public void showToast(String str) {
textView.setText(str);
toast.show();
isShowToast = true;
}
在thread的handler中,在需要时进行调用
activity.showToast("有一个提示信息");
在activity中的onPause()里面,通知thread退出时,同时调用cancel():
if(isShowToast == true){
toast.cancel();
}