注册广播,监听网络状态的改变,时间戳,自动获取系统时间

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;
	}

效果图如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值