现在的位置: 首页 > 综合 > 正文

Handling network configuration changes

2017年12月24日 ⁄ 综合 ⁄ 共 2724字 ⁄ 字号 评论关闭

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 {
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String info=intent.getStringExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
		//read network info from intent
		NetworkInfo 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 host
			if(proxyHost==null)
				proxyHost=Proxy.getDefaultHost();
			int proxyPort=Proxy.getPort(context);//get current proxy port
			if(proxyPort==-1)
				proxyPort=Proxy.getDefaultPort();
			if(proxyHost!=null&&proxyPort>1){
				//set current values on HttpClient
				HttpHost 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

抱歉!评论已关闭.