- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开发了自己的 Chronometer 实现。我做了以下操作:
创建一个服务 (CronoService),它使用执行线程的 Runnable 对象。 Runnable 将每 0.1 秒循环一次。
将从 Ui 接收消息以启动、暂停或恢复 Chronometer 的 Messenger 对象。
Intent 将在 Runnable 对象的每个循环后广播时间。
代码是:
public class CronoService extends Service {
public static final int PARAR = 0;
public static final int EMPEZAR = 1;
public static final int ESTABLECER_TIEMPO = 2;
private static final String TAG = "BroadcastService";
public static final String BROADCAST_ACTION = "jose.planilla.mostrartiempo";
final Messenger mMessenger = new Messenger(new IncomingHandler());
private final Handler handler = new Handler();
private Intent intent;
private long mDec;
private long mTotalMilis;
private long mLastMilis;
private long mElapsedTime;
private long mCurrentMilis;
private int mSeconds;
private int mMin;
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
mCurrentMilis = System.currentTimeMillis();
mElapsedTime = mCurrentMilis - mLastMilis;
mLastMilis = mCurrentMilis;
mTotalMilis += mElapsedTime;
DisplayLoggingInfo();
handler.postDelayed(this, 100); // 0.1 seconds
Log.d("run()", String.valueOf(mTotalMilis));
}
};
@Override
public void onCreate(){
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
@Override
public void onStart(Intent intent, int startId){
mTotalMilis = intent.getLongExtra("milis", 0);
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 100); // 0.1 second
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStart()");
mLastMilis = mElapsedTime = System.currentTimeMillis();
mTotalMilis = intent.getLongExtra("milis", 0);
handler.removeCallbacks(sendUpdatesToUI);
DisplayLoggingInfo();
//handler.postDelayed(sendUpdatesToUI, 100); // 0.1 second
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return mMessenger.getBinder();
}
private void DisplayLoggingInfo() {
String tiempo;
mDec = mTotalMilis % 1000;
mDec = mDec / 100;
mSeconds = (int) (mTotalMilis / 1000);
mMin = mSeconds / 60;
mSeconds = mSeconds % 60;
tiempo = "" + mDec;
if(mSeconds < 10)
tiempo = ":0" + mSeconds + "." + tiempo;
else
tiempo = ":" + mSeconds + "." + tiempo;
if(mMin < 10 )
tiempo = "0" + mMin + tiempo;
else
tiempo = mMin + tiempo;
intent.putExtra("tiempo", tiempo);
intent.putExtra("milis", mTotalMilis);
sendBroadcast(intent);
}
@Override
public void onDestroy() {
handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
}
public void pause(){
handler.removeCallbacks(sendUpdatesToUI);
}
public void resume(){
handler.postDelayed(sendUpdatesToUI, 100);
}
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg){
switch(msg.what) {
case PARAR:
pause();
break;
case EMPEZAR:
resume();
break;
case ESTABLECER_TIEMPO:
mTotalMilis = (Long) msg.obj;
break;
default:
super.handleMessage(msg);
}
}
}
我的问题是我的 Chronometer 比普通的要慢一点。它每秒钟都会损失一点时间。我做错了什么,但我找不到问题所在。非常感谢。
编辑工作代码已更新。
最佳答案
有一种更简单的方法对我来说效果很好:使用 Sistem.get CurrentMillis()
关于Android Chronometer 自己的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8118855/
我想扩展 Chronometer 类并添加我的代码。 我怎样才能做到呢?我尝试过,但无法使用 View 字段获取 Chronometer 构造函数。我得到一个异常,很明显: ... android.v
我正在编写一个基于 android.widget.Chronometer 的简单国际象棋时钟应用程序。我将它与 setCountDown(true) 标志一起使用,所有逻辑都运行良好。每个 Chron
我有一个应用程序在其主要 Activity 中包含计时码表。 Chronometer 用作计时器。一旦设备的屏幕关闭,这个定时器应该继续运行。我注意到,一旦计时器开始运行并且我将屏幕关闭较长时间,然后
我开发了自己的 Chronometer 实现。我做了以下操作: 创建一个服务 (CronoService),它使用执行线程的 Runnable 对象。 Runnable 将每 0.1 秒循环一次。 将
我有一个应用程序,我在其中向用户显示他正在做的事情的计时器。每当 Activity 进入后台(通过主页按钮或返回)时,我都会保存该时间(以秒为单位),当 Activity 恢复时,我想继续从同一时间开
我正在尝试使用 Chronometer 在我的 Android 应用程序中创建一个恢复按钮。到目前为止,我有 resume 方法的代码 fragment : private long pauseRes
我创建了一个应用程序,它有一个切换按钮来启动和停止计时器,例如秒表。当您第一次按下切换按钮(执行 .start())时,计时器将开始计时,当您再次按下按钮(执行 .stop())时,计时器出现在屏幕上
我有一个 View ,我想在上面添加一个计时器。然后,在绘制时我想显示当前时间。这是我的做法: public class MyView extends View { private C
在Android的SDK中,为我们提供了一个计时器,这个计时器称为Chronometer,我们可以成它为Android的一个组件,同时它也具备自己独有的方法。下面我们举例介绍下这个计时器的使用以及常
我正在尝试在我的白噪音应用程序中实现计时器。 应用程序有 4 个按钮可设置不同的曲调,以及 4 个功能按钮:播放/暂停、停止、一小时和三小时。最后两个正在倒计时模式下运行。 private Chron
我遇到了这个奇怪的问题,它困扰着我。我不明白为什么我在 MainActivity 中初始化计时器时会得到这个 NullPointerException。如果我在 SecondActivity 中初始化
我一直在玩弄 Android Studio,我正在尝试使用 Chronometer 小部件实现一个简单的计时器。首先我尝试了这段代码: import android.os.Bundle; import
我在一个 Activity 中有两个计时器。First Chronometer 是耗时,Second Chronometer 是间隔时间。 例如:-我们选择了 5 分钟耗时和 30 秒间隔时间,现在我
我有一个计时器,从用户遇到该 Activity 时开始计时 我目前正在使用 onCreate 期间设置的 Chronometer(最初仅在满足某些条件时才启动)。但我需要计时器继续向上计数,直到应用程
android 中的计时器如何检查计时器是否正在运行或停止?如果开始然后我想停止它,如果不运行然后启动计时器。 最佳答案 您可以使用 bool 变量来检查这一点。当您启动计时器时,您将 bool 变量
我有这个秒表需要在 30、45 和 60 秒后播放声音。 这是我的代码: @Override protected void onCreate(Bundle savedInstanceState) {
所以我的应用有 1 个 Activity 和 4 个 fragment ,其中一个有 Chronometer 来显示经过了多少时间。 它工作正常,但我有一个问题,每次我移动到另一个 fragment
我发现如果我在 setBase(SystemClock.elapsedRealtime()) 之后立即调用 start() 它将从 00:00 开始计数(这很好),见下文: startButton.s
我试图在 10 秒时停止我的 android 计时器,但我似乎无法让它工作。 我的代码如下,任何人都可以指出我正确的方向。 谢谢。 if(chronorun == false){ c
我以编程方式创建了计时器。但我不知道如何增加计时器文本的大小。以下是我的代码。我已将初始值设置为 00:00,但这个文本看起来很小,我想增加它的大小。请帮忙。 protected void onCre
我是一名优秀的程序员,十分优秀!