使用ScheduledExecutorService延时关闭一个全屏的对话框

自定义style,设置全屏属性
<resources>  
  
    <style name="AppTheme" parent="android:Theme.Black"/>  
    <style name="processDialog" >  
        <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->   
        <item name="android:windowFullscreen">true</item>    
        <item name="android:windowIsTranslucent">false</item><!--半透明-->    
        <item name="android:windowNoTitle">true</item><!--无标题-->    
        <item name="android:windowBackground">@android:color/transparent</item><!--背景透明-->    
        <item name="android:backgroundDimEnabled">true</item><!--模糊-->    
        <item name="android:backgroundDimAmount">0.5</item>   
        <item name="android:alpha">0.3</item>   
    </style>    
  
</resources>  

代码中加载这个view并把view set到dialog上,这样全屏的dialog就完成了

mView = LayoutInflater.from(this).inflate(R.layout.process_dialog, null);  
processDialog = new Dialog(context, R.style.processDialog);  
processDialog.setCancelable(false);
processDialog.setContentView(mView);  
mAutoCloseDialog = new AutoCloseDialog(processDialog);
mAutoCloseDialog.show(Prefs.DIALOG_DISPLAY_TIME);


接下来用一个封装好的类,做一个延时关闭的效果

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.app.Dialog;

public class AutoCloseDialog{  
    
    private Dialog dialog;  
    private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();  
      
    public AutoCloseDialog(Dialog dialog){  
        this.dialog = dialog;  
    }  
      
    public void show(long duration){  
    	
        Runnable runner = new Runnable() {  
            public void run() {  
                dialog.dismiss();  
            }  
        };  
        executor.schedule(runner, duration, TimeUnit.MILLISECONDS);
        dialog.show();
    }  
      
} 


其他用法示例:
Here is a class with a method that sets up a ScheduledExecutorService to beep every ten seconds for an hour:

import static java.util.concurrent.TimeUnit.*;
 class BeeperControl {
    private final ScheduledExecutorService scheduler =
       Executors.newScheduledThreadPool(1);

    public void beepForAnHour() {
        final Runnable beeper = new Runnable() {
                public void run() { System.out.println("beep"); }
            };
        final ScheduledFuture<?> beeperHandle =
            scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
        scheduler.schedule(new Runnable() {
                public void run() { beeperHandle.cancel(true); }
            }, 60 * 60, SECONDS);
    }
 }


ScheduledExecutorService执行周期性或定时任务
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值