如何在应用程序标题中显示不断更新的时间

时间:2013-08-01 05:24:19

标签: android jquery-mobile cordova

我正在开发一个Android应用程序,我正在使用phonegap。我也在使用jquery mobile。现在我想在应用程序标题的左上角添加一个时钟或当前时间(更新/更改)。

您能否建议我实现此目的或使用现有库的方法。我也可以编码,但不要重新发明轮子。

2 个答案:

答案 0 :(得分:1)

如果准确性不是很重要,你可以使用处理程序和postDelayed()。

    Handler handler = new Handler();

    void timer() {
        //Update UI with current time then post a new runnable to run after 1000ms

        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
              timer();
           }
        }, 1000);
        return;
    }

答案 1 :(得分:0)

public class Header extends LinearLayout {

private LayoutInflater inflater;
private Activity foot_activity;
protected TextView txtCurrentTime;

public Header(Context context) {
    super(context);
}

public Header(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public Header(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public void setActivity(Activity activity) {
    foot_activity = activity;
    inflateHeader();
}

private void inflateHeader() {
    inflater = (LayoutInflater) getContext().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.foot, this);
    Runnable runnable = new CountDownRunner();
    Thread myThread = new Thread(runnable);
    myThread.start();
}

class CountDownRunner implements Runnable {
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                doWork();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

public void doWork() {
    foot_activity.runOnUiThread(new Runnable() {
        public void run() {
            try {
                txtCurrentTime = (TextView) findViewById(R.id.lbltimes);
                Calendar c = Calendar.getInstance();
                SimpleDateFormat df = new SimpleDateFormat(
                        "dd-MM-yyyy HH:mm:ss a");
                String formattedDate = df.format(c.getTime());
                txtCurrentTime.setText(formattedDate);
                //System.out.println("TIME is : " + formattedDate);
            } catch (Exception e) {
            }
        }
    });
}

}

要在所需活动中检索它,只需包含以下代码段:

Header obj = (Header) findViewById(R.id.footer);
obj.setActivity(this);

有礼貌:POOVIZHI RAJAN !!!