获取系统时间
2012-07-03 14:32
127 查看
one step: 通过广播接收系统时间
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(Intent.ACTION_TIME_CHANGED.equals(action)
||Intent.ACTION_TIME_TICK.equals(action)
||Intent.ACTION_TIMEZONE_CHANGED.equals(action)){
if(handler!=null){
handler.sendEmptyMessage(SYSTEM_TIME);
}
}
}
};
two step:在handler中显示时间
timeText.setText(getSystemTime());
three step:获得系统时间的方法
public String getSystemTime(){
Calendar c = Calendar.getInstance();
ContentResolver cv = getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);
SimpleDateFormat sdf = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
if("24".equalsIgnoreCase(strTimeFormat)){
sdf.applyPattern("HH:mm");
}else{
sdf.applyPattern("hh:mm");
}
String time = sdf.format(c.getTimeInMillis());
return time;
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(Intent.ACTION_TIME_CHANGED.equals(action)
||Intent.ACTION_TIME_TICK.equals(action)
||Intent.ACTION_TIMEZONE_CHANGED.equals(action)){
if(handler!=null){
handler.sendEmptyMessage(SYSTEM_TIME);
}
}
}
};
two step:在handler中显示时间
timeText.setText(getSystemTime());
three step:获得系统时间的方法
public String getSystemTime(){
Calendar c = Calendar.getInstance();
ContentResolver cv = getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);
SimpleDateFormat sdf = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
if("24".equalsIgnoreCase(strTimeFormat)){
sdf.applyPattern("HH:mm");
}else{
sdf.applyPattern("hh:mm");
}
String time = sdf.format(c.getTimeInMillis());
return time;
}
相关文章推荐
- C++获取系统时间
- VC 获取系统时间
- JAVA中获取当前系统时间
- 32位程序在64位系统上获取系统安装时间
- C获取系统时间的方法(linux下)
- MFC 获取系统时间 CTime
- flex中如何获取系统时间
- Java获取系统时间的几种方法
- Android 获取当前系统时间
- Java获取系统时间少8个小时
- android 获取系统当前时间
- C/C++获取当前系统时间
- COleDateTime类获取系统当前时间的成员函数
- cocos2d-x酱油笔记之获取系统时间
- android 日期时间格式转换;软键盘显示消失;获取系统title
- 获取系统时间并动态显示
- C语言获取系统时间的函数
- 解决JAVA获取Windows系统时间问题
- java 获取系统时间
- NSIS 获取系统时间