注册广播,监听网络状态的改变,时间戳,自动获取系统时间
2014-09-16 11:23
627 查看
Android有一个自动获取网络时间的功能, 需要联网进行。
当我们进行联网的时候, 可以通过注册一个广播进行监听, 同时你也可以加入时间戳的intent来时间实时更新,不用开辟线程获取时间
效果图如下:
当我们进行联网的时候, 可以通过注册一个广播进行监听, 同时你也可以加入时间戳的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; }
效果图如下:
相关文章推荐
- Android通过使用系统广播监听网络状态的改变
- Android 广播BroadcastReceiver静态/动态注册,获取系统电量广播【开机广播】,获取网络状态广播,Timer的使用简介
- 关于网络状态改变监听广播的使用(android不同系统版本会有不同)
- 【Android 】注册广播,实时监听网络连接与断开状态变化
- Android 通过注册广播,实时监听网络连接与断开状态的变化
- 监听Package的安装和卸载、电源的连接和断开、网络状态改变广播、解锁广播、屏幕变亮变按广播
- Android动态注册广播,监听网络状态变化,判断网络模式
- android网络监听状态改变收到2个广播处理
- android动态注册广播,监听网络状态变化,判断网络模式
- 用服务和广播去监听系统网络状态的连接操作
- 在Widget里获取系统时间改变的广播
- Android 注册广播,实时监听网络连接与断开状态变化
- Android使用广播监听网络状态变化 一直收到系统广播 广播过滤
- 动态注册广播,监听网络状态变化,判断网络模式
- Android使用广播监听网络状态变化 一直收到系统广播
- Android中系统设置参数改变监听(以时间同步为例)
- android 使用广播监听网络状态
- Windows Phone 8 获取与监听网络连接状态 推荐
- 获取和改变Android手机网络状态的代码实现
- android接收广播监听网络状态变化