记录总结自己的工作
分类: 嵌入式
2012-09-06 11:20:53
| 1 | public static final String CONNECTIVITY_CHANGE_ACTION ="android.net.conn.CONNECTIVITY_CHANGE"; |
| 2 | private void registerDateTransReceiver() { |
| 3 | Log.i(TAG, "register receiver " + CONNECTIVITY_CHANGE_ACTION); |
| 4 | IntentFilter filter = new IntentFilter(); |
| 5 | filter.addAction(CONNECTIVITY_CHANGE_ACTION); |
| 6 | filter.setPriority(1000); |
| 7 | registerReceiver(new MyReceiver(), filter); |
| 8 | } |
| 1 | @Override |
| 2 | public void onReceive(Context context, Intent intent) { |
| 3 | String action = intent.getAction(); |
| 4 | Log.i(TAG, "PfDataTransReceiver receive action " + action); |
| 5 | if(TextUtils.equals(action, CONNECTIVITY_CHANGE_ACTION)){//网络变化的时候会发送通知 |
| 6 | Log.i(TAG, "网络变化了"); |
| 7 | return; |
| 8 | } |
| 9 | } |
| 01 | public static NetworkInfo getActiveNetwork(Context context){ |
| 02 | if (context == null) |
| 03 | return null; |
| 04 | ConnectivityManager mConnMgr = (ConnectivityManager) context |
| 05 | .getSystemService(Context.CONNECTIVITY_SERVICE); |
| 06 | if (mConnMgr == null) |
| 07 | return null; |
| 08 | NetworkInfo aActiveInfo = mConnMgr.getActiveNetworkInfo(); // 获取活动网络连接信息 |
| 09 | return aActiveInfo; |
| 10 | } |