当前位置:   article > 正文

android 实时更新时间

android studio实时更新数据库的时间

今天跟大家来分享一下如何在手机屏幕实时更新系统时间。

package com.cn;

import java.text.SimpleDateFormat;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActitvity extends Activity {
	/** Called when the activity is first created. */

	private static SimpleDateFormat currentTime;
	private static TextView systemTime;
	private LooperThread mClockThread;
	private static String date;

	public static Handler m_Handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				systemTime.setText(date);
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);
		systemTime = (TextView) findViewById(R.id.systemTime);
		mClockThread = new LooperThread();
		mClockThread.start();

	}

	class LooperThread extends Thread {
		@Override
		public void run() {
			super.run();
			try {
				do {
					currentTime = new SimpleDateFormat(
							"yyyy-MM-dd  HH:mm:ss  E");
					date = currentTime.format(new java.util.Date());
					Thread.sleep(1000);
					Message m = new Message();
					MainActitvity.m_Handler.sendMessage(m);
				} while (!LooperThread.interrupted());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >
   
 <TextView  
    android:id="@+id/systemTime"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="20dip"
    android:textColor="#99ccff"
    style="?android:attr/windowTitleStyle"
    />
   
</LinearLayout>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/679957
推荐阅读
相关标签
  

闽ICP备14008679号