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;
}
}
TimeOutHelper.java 定时常用类
最新推荐文章于 2023-04-14 12:13:59 发布