赞
踩
今天跟大家来分享一下如何在手机屏幕实时更新系统时间。
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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。