Handling network configuration changes
来源:互联网 发布:kako女装淘宝旗舰店 编辑:程序博客网 时间:2024/05/27 21:46
In any event, you must somehow get hold of these settings, and be notified when, for example, the proxy server changes while your application is running, so that you can update HttpClient to send requests through the new proxy server.
Being on the move means that network configuration may change while your application is running. To make sure that your networking code remains functional, you
must handle changes to these settings.
ConnectivityManager. In these events, ConnectivityManager will send a broadcast message to all subscribers, including the details of the network state change. Broadcast messages in Android are sent using broadcast Intents, so you need a BroadcastReceiver object to handle them. First, let’s look at how you’d register such a receiver from an Activity:
MyMovies.java
public void onCreate(Bundle savedInstanceState) {…registerReceiver(new ConnectionChangedBroadcastReceiver(),new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));}As you can see, we have to subscribe to the CONNECTIVITY_ACTION of the Connection-Manager using an IntentFilter which makes sure that we only receive broadcast
Intents of that particular kind because that’s the only thing we’re interested in for this technique.
For an application to receive broadcast events about connectivity changes, it must request the ACCESS_NETWORK_STATE permission in AndroidManifest.
A BroadcastReceiver that handles network configuration changes
public class ConnectionChangedBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubString info=intent.getStringExtra(ConnectivityManager.EXTRA_EXTRA_INFO);//read network info from intentNetworkInfo nwInfo=intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);Log.d("Connectivity change", info + ": " + nwInfo.getReason());HttpParams httpParams=MyMovies.getHttpClient().getParams();if(nwInfo.getType()==ConnectivityManager.TYPE_MOBILE){String proxyHost=Proxy.getHost(context);//get current proxy hostif(proxyHost==null)proxyHost=Proxy.getDefaultHost();int proxyPort=Proxy.getPort(context);//get current proxy portif(proxyPort==-1)proxyPort=Proxy.getDefaultPort();if(proxyHost!=null&&proxyPort>1){//set current values on HttpClientHttpHost proxy=new HttpHost(proxyHost,proxyPort);httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);}else{httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);}}else{httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);}}}
The NetworkInfo object is worth looking at more closely.
for logging, the most helpful thing is its toString method because it includes all this data already. It’ll append all the information it carries into a text line useful for debug
logs, which can be great when testing your application in different mobile environments. Here’s how such a line looks:
NetworkInfo: type: mobile[UMTS], state: CONNECTED/CONNECTED, reason: simLoaded, extra: internet, roaming: false, failover: false, isAvailable:true
- Handling network configuration changes
- Handling Configuration Changes with Fragments
- Handling Configuration Changes with Fragments
- Handling Configuration Changes with Fragments
- Handling runtime changes(二)自己处理Configuration change
- [翻译]使用Fragment处理配置更改(Handling Configuration Changes With Fragments)
- Handling Runtime Changes
- Android Handling Runtime Changes
- Handling Runtime Changes
- Handling Runtime Changes
- Android Handling Runtime Changes
- Handling Runtime Changes
- Handling Runtime changes from google
- Android Developer : Handling Runtime Changes
- 【Android】Configuration Changes
- Activity configuration changes
- Configuration Handling - SolidWorks
- Handling Runtime Changes(retain Fragment)
- 九度OJ 题目1066:字符串排序
- 开根号
- 影响Java EE性能的十大问题
- Hive原理与不足
- How Many Fibs? hdu1316 高精度
- Handling network configuration changes
- 中文转UTF
- 百度地图SDK for Android【简介】
- 利用ffmpeg将无命名规则的静态图片合成视频
- zookeeper 权限概述
- 关于GCD
- Java和C++的区别
- C#几种实现MD5加密
- 微软IE的HTML 组件(.htc)文件。