获取系统时间
来源:互联网 发布:超次元矩阵官网公测 编辑:程序博客网 时间:2024/06/12 21:12
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;
}
- linux时间-获取系统时间
- 获取(设置)系统时间
- jsp获取系统时间
- 获取系统时间
- VC--获取系统时间
- 获取系统时间
- 获取系统当前时间
- 获取系统时间
- 获取当前系统时间
- 获取系统时间
- 获取系统时间
- C++ 获取系统时间
- oracle 获取系统时间
- java获取系统时间
- 获取系统时间代码
- 获取系统时间
- 获取系统时间
- 获取当前系统时间
- RTP http://www.cnblogs.com/qingquan/archive/2011/07/28/2120440.html
- button/input链接方式全攻略
- The connection to adb is down, and a severe error has occured.
- #define STRICT
- Lua学习笔记(4)--继承的尝试(1)
- 获取系统时间
- 正向代理和反向代理
- Android异步处理四:AsyncTask的实现原理
- 渲染一个简单的房间
- Linq to sql 数据库连接问题
- Ubuntu下调整swap分区的大小
- Android编译过程详解
- openSUSE 开启 SSHD
- Java--图片中动态添加文字