2
我開發了一個自己的天文臺實現。我做了如下:Android天文臺自己實現
創建服務(CronoService),使用該執行的線程Runnable對象。 Runnable將循環每個0.1秒。
Messenger對象將接收來自Ui的消息以啓動,暫停或恢復計時器。
將在每個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);
}
}
}
我的問題是,我的天文臺表比正常的速度慢了一點。它每秒鐘都會丟失一點時間。 我做錯了什麼,但我找不到問題。 非常感謝。
編輯 更新了工作代碼。
感謝您的快速回答ArcDare,但我認爲這是沒有問題的。我曾經設置了原始時間(以毫秒爲單位),並從這個時間開始計算。我認爲問題是我在每個循環的任何時刻都會浪費時間。 – gutiory
我吃了我的話ArcDare。你完全正確。我有點厭倦工作,我看不到你給我的解決方案。非常感謝。 – gutiory
很高興聽到它終於工作:) – ArcDare