android关闭弹窗,Android 弹出窗口 定时关闭

工作中正需要一个弹出窗口的定时关闭,考虑了下可以使用Handler,Timer或者AsyncTask基础结合实现。不过,偶然发现了原来Android的api竟然已有类似功能的实现类CountDownTimer。该类实现了在指定时间间隔进行倒计时功能。

查看Android的SDK,看到该类

public abstract class

CountDownTimer extends

Object

android.os.CountDownTimer

是一个抽象类。运行在应用的子线程。主要的方法:

publicvoidonFinish() { }

尽管CountDownTimer类是运行在应用的子线程,但上面实现的两个方法却是运行在主线程的。效果图:

0818b9ca8b590ca3270a3433284dd417.png

下面上代码:

public class CountDownDialog

{

private Activity _activity;

private LayoutInflater _inflater;

private View _layout;

private View _anchor;

private TextView tv_tips;

private PopupWindow _popupWindow;

private CountTimer countTimer;

/**

* 构造函数

* @param activity上下文对象

* @param anchor 锚点

* @param millisInFuture 倒计的时间数,以毫秒为单位

* @param countDownInterval 倒计每秒中间的间隔时间,以毫秒为单位

*/

public CountDownDialog(Activity activity,View anchor,long millisInFuture, long countDownInterval)

{

_activity = activity;

_anchor = anchor;

_inflater = (LayoutInflater) _activity.getSystemService("layout_inflater");

_layout = (LinearLayout) _inflater.inflate(R.layout.count_dialog, null, true);

tv_tips = (TextView)_layout.findViewById(R.id.tv_countdialog_tips);

countTimer = new CountTimer(millisInFuture,countDownInterval);

}

public void show()

{

if(_popupWindow == null)

{

_popupWindow = new PopupWindow(_layout,250, 200,true);

}

_popupWindow.showAtLocation(_anchor, Gravity.CENTER, 0, 0);

_popupWindow.update();

countTimer.start();

}

public void cancle()

{

if(_popupWindow != null && _popupWindow.isShowing())

{

countTimer.cancel();

_popupWindow.dismiss();

if(_activity != null)

{

_activity.finish();

}

}

}

class CountTimer extends CountDownTimer

{

/**

* 构造函数

* @param millisInFuture 倒计的时间数,以毫秒为单位

* @param countDownInterval 倒计每秒中间的间隔时间,以毫秒为单位

*/

public CountTimer(long millisInFuture, long countDownInterval)

{

super(millisInFuture, countDownInterval);

}

@Override

public void onFinish()

{

cancle();

}

@Override

public void onTick(long millisUntilFinished)

{

tv_tips.setText(_activity.getString(R.string.issaving)+millisUntilFinished / 1000 + _activity.getString(R.string.second));

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 中实现在悬浮之下或者在新之下,您可以使用 WindowManager.LayoutParams 类的 flags 属性来设置窗口类型。 1. 在悬浮之下 如果您希望位于悬浮之下,您可以将 WindowManager.LayoutParams 类的 flags 属性设置为 FLAG_NOT_FOCUSABLE 和 FLAG_NOT_TOUCH_MODAL。 ```java WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT); ``` 2. 在新之下 如果您希望位于新之下,您可以将 WindowManager.LayoutParams 类的 flags 属性设置为 FLAG_NOT_FOCUSABLE、FLAG_NOT_TOUCH_MODAL 和 FLAG_WATCH_OUTSIDE_TOUCH。 ```java WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); ``` 在这种情况下,您需要在中添加 FLAG_NOT_FOCUSABLE 和 FLAG_NOT_TOUCH_MODAL 标志,以确保它们不会干扰您的。例如: ```java WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT); ``` 注意:使用 WindowManager.LayoutParams 类的 flags 属性需要您的应用程序具有 SYSTEM_ALERT_WINDOW 权限。为了避免安全风险,您需要在 Android 6.0 及更高版本中请求此权限,并且用户必须授予此权限才能使用该功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值