Android有一个自动获取网络时间的功能, 需要联网进行。
当我们进行联网的时候, 可以通过注册一个广播进行监听, 同时你也可以加入时间戳的intent来时间实时更新,不用开辟线程获取时间
IntentFilter netFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
netFilter.addAction(NET_STATE_CHANGE_ACTION);
netFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
netFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
netFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
netFilter.addAction(Intent.ACTION_TIME_TICK);
registerReceiver(mNetReceiver, netFilter);
/**
* A broadcast to listener net state changed
*/
private BroadcastReceiver mNetReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null)
return;
// ------------------------get net type----------------------------
State wifiState = null;
State mobileState = null;
State ethernetState = null;
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
ethernetState = cm
.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET)
.getState();
if (wifiState != null && mobileState != null
&& ethernetState != null && State.CONNECTED != wifiState
&& State.CONNECTED != ethernetState
&& State.CONNECTED == mobileState) {
// no net
} else if (wifiState != null && ethernetState != null
&& State.CONNECTED != ethernetState
&& State.CONNECTED == wifiState) {
// wifi connected
} else if (wifiState != null && ethernetState != null
&& State.CONNECTED != wifiState
&& State.CONNECTED == ethernetState) {
// ethernet connected
}
// -----------------------end---------------------------------
Log.d(TAG, "netReceiver action = " + action);
if (action.equals(Intent.ACTION_TIME_TICK)) {
displayDate();
time_count++;
if (time_count >= 60) {
time_count = 0;
}
} else {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
if (Utils.isConnectingToInternet(LauncherForDefault.this)) {
Settings.System.putInt(getContentResolver(),
Settings.System.AUTO_TIME, 0);
Settings.System.putInt(getContentResolver(),
Settings.System.AUTO_TIME, 1);
setDate();
} else {
Utils.makeCustomToast(LauncherForDefault.this,
R.string.hint_net_disable, 6000);
}
}
}
}
};
/**
* show date message
*/
private void displayDate() {
int[] array = getTime();
ImageView hour_1 = (ImageView) findViewById(R.id.img_hour_1);
ImageView hour_2 = (ImageView) findViewById(R.id.img_hour_2);
ImageView colon = (ImageView) findViewById(R.id.img_colon);
ImageView minu_1 = (ImageView) findViewById(R.id.img_minu_1);
ImageView minu_2 = (ImageView) findViewById(R.id.img_minu_2);
ImageView day = (ImageView) findViewById(R.id.img_day);
setImage(hour_1, array[0]);
setImage(hour_2, array[1]);
colon.setImageResource(R.drawable.s_colon);
setImage(minu_1, array[2]);
setImage(minu_2, array[3]);
LauncherForDefault.is24hFormart = DateFormat.is24HourFormat(this);
if (array[4] != -1) {
day.setVisibility(View.VISIBLE);
if (array[4] == 0) {
day.setImageResource(R.drawable.s_am);
} else {
day.setImageResource(R.drawable.s_pm);
}
} else {
day.setVisibility(View.GONE);
}
}
/**
* get the date and week
*/
public void setDate() {
Calendar mCalendar = Calendar.getInstance();
int year = mCalendar.get(Calendar.YEAR);
int month = mCalendar.get(Calendar.MONTH) + 1;
int date = mCalendar.get(Calendar.DATE);
int dayOfWeek = mCalendar.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek < 0) {
dayOfWeek = 0;
}
mTvDate.setText(date + " ");
mTvDay.setText(weekDays[dayOfWeek]);
mTvMonth.setText(month + " . ");
mTvYear.setText(year + "");
if(year == 1970){
timeFlag = true;
}else{
timeFlag = false;
}
if (Utils.isZh(mContext)) {
String lunarStr = "";
Lunar lunar = new Lunar(mCalendar);
lunarStr = lunar.animalsYear() + "(" + lunar.cyclical() + ")年"
+ lunar.toString();
mLunarTv.setText(lunarStr);
} else {
mLunarTv.setVisibility(View.GONE);
}
mCalendar.clear();
mCalendar = null;
}
效果图如下: