TimeOutHelper.java 定时常用类

package com.mstar.tvsettings.ui.program.pvr;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

public class TimeOutHelper implements Runnable
{
    private final static int TIME_OUT_MSG = 51;
    private int totalTime = 10;
    private int count = totalTime;
    private Handler handler;
    private boolean enable = false;
    private boolean isStop = true;
    private Context mContext;
    private final String TAG = "TimeOutHelper";

    public void setValue(int second)
    {
        totalTime = second;
        count = totalTime;
    }

    public static int getTimeOutMsg()
    {
        return TIME_OUT_MSG;
    }

    public void reset()
    {
        count = totalTime;
    }

    public void resume()
    {
        enable = true;
        count = totalTime;
    }

    public void pause()
    {
        enable = false;
    }

    public void stop()
    {
        isStop = true;
    }

    public TimeOutHelper(Handler handler, Context mContext)
    {
        isStop = true;
        enable = true;
        this.handler = handler;
        this.mContext = mContext;
    }

    public void start()
    {
        if (isStop)
        {
            isStop = false;
            new Thread(this).start();
        }
    }

    private void decrease()
    {
        count--;
    }

    @Override
    public void run()
    {
        while (!isStop)
        {
            try
            {
                Thread.currentThread();
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            if (enable)
            {
                if (count == 0)
                {
                    handler.sendEmptyMessage(TIME_OUT_MSG);
                }
                decrease();
            }
        }
    }
    
    public void init()
    {
        int remainingTime = 0 ;
        remainingTime = getTimeOutFormMenuDB();
        //No residual time?
        if (remainingTime != 5 && remainingTime != 10 && remainingTime != 15
                && remainingTime != 20 && remainingTime != 30)
        {
            isStop = true;
        }
        else
        {
            setValue(remainingTime);
        }
    }
    
    private int getTimeOutFormMenuDB()
    {
        int remainingTime = 0;
        int getDBTime = 0;
        Cursor cursor = null;
        try
        {
            cursor = mContext.getContentResolver().query(Uri.parse("content://mstar.tv.usersetting/systemsetting")
                    , null, null, null, null);
            if (cursor.moveToFirst())
            {
                getDBTime = (int)cursor.getLong(cursor.getColumnIndex("u32MenuTimeOut"));
            }
            
            remainingTime = (int) (getDBTime/1000);
            Log.d(TAG, "remainingTime..."+remainingTime);
            
            return remainingTime;
        }
        catch(Exception e)
        {
            Log.d(TAG, e.getMessage());
        }
        finally
        {
            cursor.close();
        }
        return 0;
        
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值