1,模拟时钟和数字时钟的直接调用
2,用一个TextView显示获取的当前时间
3,修改系统默认的时间格式为24小时制
package irdc.EX04_14_B;
/*
* @author Modfiy by jake20001@126.com
* */
import android.app.Activity;
import android.content.ContentResolver;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle; /*这里我们需要使用Handler类别与Message类别来处理线程*/
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.AnalogClock;
import android.widget.DigitalClock;
import android.widget.TextView; /*需要使用Java的Calendar与Thread类别来取得系统时间*/
import java.util.Calendar;
import java.lang.Thread;
public class EX04_14_B extends Activity
{ /* 宣告一常数作为判别讯息用 */
protected static final int GUINOTIFIER = 0x1234; /* 宣告两个widget对象变量 */
protected static final String TAG = "EX04_14";
private TextView mTextView;
public AnalogClock mAnalogClock; /* 宣告与时间相关的变量 */
public Calendar mCalendar;
public int mMinutes;
public int mHour; /* 宣告关键Handler与Thread变量 */
public Handler mHandler;
private Thread mClockThread;
public DigitalClock mDigitalClock;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /* 透过findViewById取得两个widget对象 */
mTextView = (TextView) findViewById(R.id.myTextView);
mAnalogClock = (AnalogClock) findViewById(R.id.myAnalogClock); /* 透过Handler来接收线程所传递的信息并更新TextView */
mDigitalClock = (DigitalClock) findViewById(R.id.myDigitalClock);
Resources res = getResources();
Configuration conf = res.getConfiguration();
ContentResolver cv = this.getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);
if(strTimeFormat.equals("12"))
{
Log.i(TAG,"12");
android.provider.Settings.System.putString(cv, android.provider.Settings.System.TIME_12_24, "24");
}
mHandler = new Handler()
{
public void handleMessage(Message msg)
{ /* 这里是处理信息的方法 */
switch (msg.what)
{
case EX04_14_B.GUINOTIFIER: /* 在这处理要TextView对象Show时间的事件 */
mTextView.setText(mHour + " : " + mMinutes);
Log.i(TAG,"2222222222222222");
break;
}
super.handleMessage(msg);
}
}; /* 透过线程来持续取得系统时间 */
mClockThread = new LooperThread();
Log.i(TAG,"000000000000000000");
mClockThread.start();
} /* 改写一个Thread Class用来持续取得系统时间 */
class LooperThread extends Thread
{
public void run()
{
super.run();
try
{
do
{ /* 取得系统时间 */
long time = System.currentTimeMillis(); /* 透过Calendar对象来取得小时与分钟 */
final Calendar mCalendar = Calendar.getInstance();
mCalendar.setTimeInMillis(time);
mHour = mCalendar.get(Calendar.HOUR);
mMinutes = mCalendar.get(Calendar.MINUTE); /* 让线程休息一秒 */
Thread.sleep(1000); /* 重要关键程序:取得时间后发出讯息给Handler */
Message m = new Message();
m.what = EX04_14_B.GUINOTIFIER;
mHandler.sendMessage(m);
Log.i(TAG,"11111111111111111");
} while (EX04_14_B.LooperThread.interrupted() == false); /* 当系统发出中断讯息时停止本循环 */
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
在layout的xml下加入系统自带的2个时钟类型
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget27"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<AnalogClock
android:id="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
</AnalogClock>
<DigitalClock
android:id="@+id/myDigitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
></DigitalClock>
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"
android:textColor="@drawable/white"
android:layout_gravity="center_horizontal"
>
</TextView>
</LinearLayout>
加写系统的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="irdc.EX04_14_B"
android:versionCode="1"
android:versionName="1.0.0">
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="irdc.EX04_14_B.EX04_14_B"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>