Android的天文台表自己的实现 [英] Android Chronometer own implementation

查看:168
本文介绍了Android的天文台表自己的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了自己的实施天文台表。我做了如下:

I've developed an own implementation of a Chronometer. I did the follow:


  1. 创建服务(CronoService),使用该执行的线程Runnable对象。可运行的循环将每0.1秒。

  1. Create a Service (CronoService), that use a Runnable object that execute the thread. The Runnable will loop each 0.1 secs.

Messenger的对象,将收到来自用户界面的消息开始,暂停或恢复天文台表。

Messenger Object that will receive messages from Ui to Start, Pause or Resume the Chronometer.

意图将播出Runnable对象的每个循环后的时间。

Intent that will broadcast the time after each loop of the Runnable object.

在code是:

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);
        }
    }
}

我的问题是,我的天文台表比正常的要慢一点。它失去的时间连接每秒O位。
我做错了什么,但我不能发现问题。
非常感谢。

My problem is that the my Chronometer is a bit slower than a normal one. It loses o bit of time en each seconds. I'm doing something wrong, but I can't find the problem. Thanks a lot.

修改
工作code更新。

EDIT Working code updated.

推荐答案

有这么为我工作的罚款更简单的方法:用 Sistem.getCurrentMillis打()

There's an easier way that has worked fine for me: playing with Sistem.getCurrentMillis()

看看这个:
安卓:?天文台用毫秒

这篇关于Android的天文台表自己的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆