当前位置:首页 » 安卓系统 » 安卓中的计时器属于什么线程

安卓中的计时器属于什么线程

发布时间: 2023-05-23 14:42:14

Ⅰ android中怎么使timecount自动开始计时

android计时器,时间计算器的实现方法,需要的朋友可以参考一下

需求:默认为"00:00:00",点击开始按钮时清零后开始计时,出现如10:28:34。点击停止的时候停止计时。
问题:使用Calendar
DateFormat的方法,不设置时区获取到的小时是本地时区的(东八区的就是8),设置成GMT标准时区获取到的时间是12小时(12:00:00),设置24小时制无效。
在开始时间加减各种小时都无效,而且计时只能到12小时就自动跳上去了,始终无法出现默认状态00:00:00开始计时的效果。
尝试各种时间设置方法无效后只能自己写一个根据秒数转换时间格式字符串的方法了,经过测试是没问题的,两位数只能显示99小时为最大,如需要更大小时数需要改改方法。
另外小时数也不能无限大,超过long数据类型长度会变成负数的,会出现异常的。

显示效果:

测试类:

复制代码
代码如下:

public class TestTime {
public
static void main(String[] args) {
TestTime tt = new
TestTime();
tt.showTimeCount(99*3600000+75*1000);

}

//时间计数器,最多只能到99小时,如需要更大小时数需要改改方法
public String
showTimeCount(long time) {

System.out.println("time="+time);
if(time >=
360000000){
return "00:00:00";
}
String
timeCount = "";
long hourc = time/3600000;
String hour =
"0" + hourc;
System.out.println("hour="+hour);
hour =
hour.substring(hour.length()-2, hour.length());

System.out.println("hour2="+hour);

long minuec =
(time-hourc*3600000)/(60000);
String minue = "0" +
minuec;
System.out.println("minue="+minue);
minue =
minue.substring(minue.length()-2, minue.length());

System.out.println("minue2="+minue);

long secc =
(time-hourc*3600000-minuec*60000)/1000;
String sec = "0" +
secc;
System.out.println("sec="+sec);
sec =
sec.substring(sec.length()-2, sec.length());

System.out.println("sec2="+sec);
timeCount = hour + ":" + minue +
":" + sec;
System.out.println("timeCount="+timeCount);

return timeCount;
}

}

实际例子:

复制代码
代码如下:

//时间计数器,最多只能到99小时,如需要更大小时数需要改改方法

public String showTimeCount(long time) {
if(time >=
360000000){
return "00:00:00";
}
String
timeCount = "";
long hourc = time/3600000;
String hour =
"0" + hourc;
hour = hour.substring(hour.length()-2,
hour.length());

long minuec =
(time-hourc*3600000)/(60000);
String minue = "0" +
minuec;
minue = minue.substring(minue.length()-2,
minue.length());

long secc =
(time-hourc*3600000-minuec*60000)/1000;
String sec = "0" +
secc;
sec = sec.substring(sec.length()-2, sec.length());

timeCount = hour + ":" + minue + ":" + sec;
return
timeCount;
}

private Handler stepTimeHandler;

private Runnable mTicker;
long startTime = 0;

//开始按钮

class startBtnListener implements OnClickListener {

@Override
public void onClick(View v) {
Button b =
(Button)v;
String buttonText =
b.getText().toString();

if("Start".equalsIgnoreCase(buttonText)){

b.setText("Stop");
// 清零 开始计时

stepTimeTV.setText("00:00:00");
stepTimeHandler = new
Handler();
startTime =
System.currentTimeMillis();
mTicker = new Runnable()
{
public void run() {
String
content = showTimeCount(System.currentTimeMillis() -
startTime);

stepTimeTV.setText(content);

long now =
SystemClock.uptimeMillis();
long next = now + (1000
- now % 1000);
stepTimeHandler.postAtTime(mTicker,
next);
}
};

//启动计时线程,定时更新
mTicker.run();

}else{
b.setText("Start");
//停止计时 Remove
any pending posts of Runnable r that are in the message
queue.

stepTimeHandler.removeCallbacks(mTicker);
}

}
}

用时间格式化的方式测试代码:

复制代码
代码如下:

//开始按钮 通过Calendar时间设置的方式,无法正常显示小时为0

class startBtnListener implements OnClickListener {

@Override
public void onClick(View v) {
Button b =
(Button)v;
String buttonText =
b.getText().toString();

if("Start".equalsIgnoreCase(buttonText)){

b.setText("Stop");
// 清零 开始计时

stepTimeTV.setText("00:00:00");
if (mCalendar == null)
{
mCalendar =
Calendar.getInstance();
TimeZone tz =
TimeZone.getTimeZone("GMT");//GMT+8

mCalendar.setTimeZone(tz);

mCalendar.get(Calendar.HOUR_OF_DAY);//24小时制

}
stepTimeHandler = new Handler();

//System.uptimeMillis()
//记录从机器启动后到现在的毫秒数,当系统进入深度睡眠时,此计时器将会停止

//System.currentTimeMillis()
//返回自1970年1月1日到现在的毫秒数,通常用来设置日期和时间
//System.elapsedRealtime()
//返回从机器启动后到现在的毫秒数,包括系统深度睡眠的时间,api里没有这个方法

//直接取得的是当地时区时间,当地时间跟时区有关,设置GMT后始终多12小时
startTime =
System.currentTimeMillis();//12*3600000 - 36*3600000减掉或者加上12小时都不行

mTicker = new Runnable() {
public
void run() {
//这个减出来的日期是1970年的 时间格式不能出现00:00:00
12:00:00
long showTime = System.currentTimeMillis()
- startTime;

Log.i(TAG,showTime+"");

mCalendar.setTimeInMillis(showTime + 13*3600000 +
1000);
String content = (String)
DateFormat.format(mFormat, mCalendar);

stepTimeTV.setText(content);

long now =
SystemClock.uptimeMillis();
long next = now + (1000
- now % 1000);
stepTimeHandler.postAtTime(mTicker,
next);
}
};

//启动计时线程,定时更新
mTicker.run();

}else{
b.setText("Start");
//停止计时 Remove
any pending posts of Runnable r that are in the message
queue.

stepTimeHandler.removeCallbacks(mTicker);
}

}
}

private Handler stepTimeHandler;
Calendar
mCalendar;
String mFormat = "yyyy-MM-dd hh:mm:ss";//yyyy-MM-dd

long startTime = 0;
private Runnable mTicker;

Ⅱ android 计时器代码

Timer mTimer = new Timer();
mTask = new TimerTask(){
public void run(){
//execute the task
Message message = new Message();
message.what = DIALOG_HIDE;
//Log.e(TAG, "message.what = DIALOG_HIDE");
mHandler.sendMessage(message);
}
};
mTimer.schele(mTask, 5000);
在主线程里开启timer,等于启动了另一个线程,线程之间的通信是要用handler发送message,然后在主线程里用handler.handleMessage()里面接收并处理。

Ⅲ android service+broadcastreceiver怎么实现秒表计时器

Service起线程监听系统时间,系统时间变化一定毫秒数之后发Broadcast,在Activity里面做broadcastreceiver来接收Service发的广播来更新显示。

如果你需要应用在后台的时候也在计时的话才需要Service,否则直接在Activity里用Handler单起一个线程也可以。

Ⅳ android中looper的实现原理,为什么调用looper.prepare就在当前线程关联了一个lo

实际上:消息发送和计划任务提交之后,它们都会进入某线程的消息队列中,我们可以把这个线程称之为目标线程。不论是主线程还是子线程都可以成为目标线程。上例中之所以在主线程中处理消息,是因为我们要更新UI,按照android中的规定我们必须由主线程更新UI。所以我们让主线程成为了目标线程。

那么如何控制让某个线程成为目标线程呢?

这就引出了Looper的概念。Android系统中实现了消息循环机制,Android的消息循环是针对线程的,每个线程都可以有自己的消息队列和消息循环。Android系统中的通过Looper帮助线程维护着一个消息队列和消息循环。通过Looper.myLooper()得到当前线程的Looper对象,通过Looper.getMainLooper()得到当前进程的主线程的Looper对象。
前面提到每个线程都可以有自己的消息队列和消息循环,然而我们自己创建的线程默认是没有消息队列和消息循环的(及Looper),要想让一个线程具有消息处理机制我们应该在线程中先调用Looper.prepare()来创建一个Looper对象,然后调用Looper.loop()进入消息循环。如上面的源码所示。
当我们用Handler的构造方法创建Handler对象时,指定handler对象与哪个具有消息处理机制的线程(具有Looper的线程)相关联,这个线程就成了目标线程,可以接受消息和计划任务了。Handler中的构造方法如下:

[java] view
plainprint?

public Handler() {

if (FIND_POTENTIAL_LEAKS) {

final Class<? extends Handler> klass = getClass();

if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&

(klass.getModifiers() & Modifier.STATIC) == 0) {

Log.w(TAG, "The following Handler class should be static or leaks might occur: " +

klass.getCanonicalName());

}

}

mLooper = Looper.myLooper();

if (mLooper == null) {

throw new RuntimeException(

"Can't create handler inside thread that has not called Looper.prepare()");

}

mQueue = mLooper.mQueue;

mCallback = null;

}

public Handler(Looper looper) {

mLooper = looper;

mQueue = looper.mQueue;

mCallback = null;

}
public Handler() {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}

mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = null;
}

public Handler(Looper looper) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = null;
}

在上述的计时器的例子中,之所以可以在主线程中处理消息而我们自己并没有调用Looper.prepare()等方法,是因为Android系统在Activity启动时为其创建一个消息队列和消息循环,当我们用无参的Handler构造方法创建对象时又用了当前线程的Looper对象,及将handler与主线程中的Looper对象进行了关联。

android中是使用Looper机制来完成消息循环的,但每次创建线程时都先初始化Looper比较麻烦,因此Android为我们提供了一个HandlerThread类,他封装了Looper对象,是我们不用关心Looper的开启和释放问题。

不管是主线程还是其他线程只要有Looper的线程,别的线程就可以向这个线程的消息队列中发送消息和任务。

我们使用HandlerThread类代替上一篇文章中的子线程,并用HandlerThread类中的Looper对象构造Handler,则接受消息的目标线程就不是主线程了,而是HandlerThread线程。代码如下:

[java] view
plainprint?

public class clockActivity extends Activity {

/** Called when the activity is first created. */

private String TAG="clockActivity";

private Button endButton;

private TextView textView;

private int timer=0;

private boolean isRunning=true;

private Handler handler;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

endButton=(Button)findViewById(R.id.endBtn);

textView=(TextView)findViewById(R.id.textview);

endButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

isRunning=false;

}

});

HandlerThread thread=new HandlerThread("myThread");

handler=new Handler(thread.getLooper());//与HandlerThread中的Looper对象关联

thread.start();

Runnable r=new Runnable(){

@Override

public void run() {

// TODO Auto-generated method stub

if(isRunning){

textView.setText("走了"+timer+"秒");

timer++;

handler.postDelayed(this, 1000);//提交任务r,延时1秒执行

}

}

};

handler.postDelayed(r, 1000);

}

}
public class clockActivity extends Activity {
/** Called when the activity is first created. */
private String TAG="clockActivity";
private Button endButton;
private TextView textView;
private int timer=0;
private boolean isRunning=true;
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
endButton=(Button)findViewById(R.id.endBtn);
textView=(TextView)findViewById(R.id.textview);
endButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isRunning=false;
}
});
HandlerThread thread=new HandlerThread("myThread");
handler=new Handler(thread.getLooper());//与HandlerThread中的Looper对象关联
thread.start();
Runnable r=new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
if(isRunning){
textView.setText("走了"+timer+"秒");
timer++;
handler.postDelayed(this, 1000);//提交任务r,延时1秒执行
}
}
};
handler.postDelayed(r, 1000);
}
}

此时处理任务会在handlerThread线程中完成。当然这个例子会出线异常:依然是因为在非主线程中更新了UI。这样做只是为了大家能够理解这种机制。

深入理解Android消息处理机制对于应用程序开发非常重要,也可以让我们对线程同步有更加深刻的认识,希望这篇文章可以对朋友们有所帮助。

热点内容
锐志哪个配置性价比最高 发布:2025-02-12 17:38:43 浏览:917
智能推送算法 发布:2025-02-12 17:38:41 浏览:834
拍照上传器 发布:2025-02-12 17:34:29 浏览:651
androidweb框架 发布:2025-02-12 17:32:45 浏览:75
安卓编程贺卡 发布:2025-02-12 17:32:44 浏览:837
php获取数据库的字段 发布:2025-02-12 17:29:02 浏览:765
服务器地址消失 发布:2025-02-12 17:23:36 浏览:950
后台执行php脚本 发布:2025-02-12 17:21:45 浏览:470
spring编程式事务 发布:2025-02-12 17:16:55 浏览:397
nginx禁止ip访问 发布:2025-02-12 17:15:14 浏览:273