1.sendmessage
handler.sendEmptyMessageDelayed(TOAST, 5*1000);//add by devin
2.message处理,10s循环弹窗
case TOAST: String CustomId = getEnvCustomId(); Log.i("SafetyCheck", "handler CHECK_AGAIN : "+CustomId+" ini : "+compareCustomId); if (!CustomId.equals(compareCustomId)) { handler.sendEmptyMessageDelayed(TOAST, 10*1000);//时间间隔后再继续显示POP_FLASH toastShow(mContext, POP_FLASH_TEXT); }else{ Log.i("SafetyCheck", "remove TOAST"); handler.removeMessages(TOAST); }
3.不能多次创建toast,只创建一次
public void toastShow(Context context, String warntext){ if(mToast == null){ Log.i("SafetyCheck", "initToast..."); initToast(context,warntext); } Log.i("SafetyCheck", "toastShow..."); mToast.show(); } public void initToast(Context context, String warntext){ mToast = Toast.makeText(context, null, Toast.LENGTH_SHORT); TextView textView = new TextView(context); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); //params.setMargins(0, 800, 0, 0); textView.setLayoutParams(params); textView.setHeight(getScreenHeight(context)/3); textView.setText(warntext); textView.setTextSize(25); textView.setGravity(Gravity.CENTER); textView.setTextColor(Color.WHITE); //textView.setAlpha(0.2f); textView.setBackgroundColor(0x7D000000);//透明 黑色 textView.setFocusable(false); mToast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 0); mToast.setView(textView); }
4.获取屏幕高度
/** * 获得屏幕高度 * * @param context * @return */ public int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 获得屏幕宽度 * * @param context * @return */ public int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; }