private boolean mShowTips = false;
private static final int FLOAT_VIEW_DISPLAY_TIME = 2000;
private void showTips(int resid) {
if (mShowTips) {
Log.w(TAG,"The floating window is showing, stop showing another one.");
return;
}
//Android toast is not able to be shown on top of notifications, so
//implement a floating window to replace it.
LayoutInflater inflate = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View floatView = inflate.inflate(
com.android.internal.R.layout.transient_notification, null);
final TextView textView = (TextView) floatView
.findViewById(com.android.internal.R.id.message);
textView.setText(ctx.getString(resid));
final WindowManager windowManager = (WindowManager) ctx
.getSystemService(ctx.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
params.format = PixelFormat.TRANSLUCENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.y = windowManager.getDefaultDisplay().getHeight()/3;
params.windowAnimations = com.android.internal.R.style.Animation_Toast;
windowManager.addView(floatView, params);
mShowTips = true;
//close notification in two seconds
new Timer().schedule(new TimerTask() {
@Override
public void run() {
windowManager.removeView(floatView);
mShowTips = false;
}
}, FLOAT_VIEW_DISPLAY_TIME);
}
模仿Toast自定义悬浮窗口
最新推荐文章于 2021-05-29 10:23:42 发布