gpt4 book ai didi

Android Chronometer 自己的实现

转载 作者:行者123 更新时间:2023-11-29 02:09:49 24 4
gpt4 key购买 nike

我开发了自己的 Chronometer 实现。我做了以下操作:

  1. 创建一个服务 (CronoService),它使用执行线程的 Runnable 对象。 Runnable 将每 0.1 秒循环一次。

  2. 将从 Ui 接收消息以启动、暂停或恢复 Chronometer 的 Messenger 对象。

  3. 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 with Milliseconds?

关于Android Chronometer 自己的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8118855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com