I. Monitor and determine the network status (forwarding: http://wang-peng1.javaeye.com/blog/557362 ):
public class ConnectionChangeReceiver extends BroadcastReceiver { @Override public void onReceive( Context context, Intent intent ) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE ); if ( activeNetInfo != null ) { Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); } if( mobNetInfo != null ) { Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); } } }
<!-- Needed to check when the network connection changes --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver" android:label="NetworkConnection"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver>
1-1: Note: official explanation of the connectivitymanager class:
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by callingContext.getSystemService(Context.CONNECTIVITY_SERVICE).
The primary responsibilities of this class are:
- Monitor network connections (wi-fi, GPRS, UMTS, etc .)
- Send broadcast intents when network connectivity changes
- Attempt to "fail over" to another network when connectivity to a network is lost
- Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks
1-2: Note: official explanation of the networkinfo class:
1-2-1 describes the status of a network interface of a given type (currently either mobile or WiFi ).
1-2-2 networkinfo. gettypename () method explanation: return a human-readable name describe the type of the network, for example "WiFi" or "mobile ".
1-3: add the permission: Android. Permission. access_network_state
2. How to judge the network connection status of Android series (to: http://tech.ddvip.com/2010-07/1280393591158260.html)
Let's discuss the technical implementation of judging the network connection status of Android phones.
Currently, most mobile phones in the Android operating system support Wi-Fi, GSM, and 3G network communication. However, you can only choose one connection method each time you connect to the network, such as the one customized by the carrier, A specific network environment (cmwap, ctwap, and so on) must be required. If you want to switch the network, you must first disable the existing network and then start a new network. This conversion process takes some time, maybe the program needs to know whether the network connection is successful and then automatically log on to the new network server. How can we know when the connection is successful? How long does it take? Maybe a thread is used to monitor whether the network status is linked successfully. We can use another method, namely, phonestatelistener, which is correct. Many applications use phonestatelistener to listen for incoming call power. phonestatelistener can also listen for network disconnection, connection, and connection success.
Final telephonymanager mtelephonymgr = (telephonymanager) getsystemservice (context. telephony_service); mtelephonymgr. listen (New phonestatelistener () {@ override public void ondataconnectionstatechanged (INT state) {Switch (state) {Case telephonymanager. data_disconnected: // network disconnection break; Case telephonymanager. data_connecting: // The network is connecting to break; Case telephonymanager. data_connected: // break on the network connection ;}}}
Phonestatelistener. listen_data_connection_state); we only need to reload the ondataconnectionstatechanged method and make corresponding processing based on the state judgment.
3. Android
Determine whether a network is available (to: http://yangguangfu.javaeye.com/blog/723334)
Public Boolean checknetwork () {Boolean flag = false; connectivitymanager cwjmanager = (connectivitymanager) androidbaseactivity. Self. getsystemservice (context. connectivity_service); If (cwjmanager. getactivenetworkinfo ()! = NULL) Flag = cwjmanager. getactivenetworkinfo (). isavailable (); If (! Flag) {builder B = new alertdialog. builder (androidbaseactivity. self ). settitle ("no available network "). setmessage (androidbaseactivity. self. getresources (). getstring (r.string.net _ work_message); B. setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {intent mintent = new intent ("/"); componentname comp = new componentname ("com. android. settings "," com. android. settings. wirelesssettings "); mintent. setcomponent (COMP); mintent. setaction ("android. intent. action. view "); androidbaseactivity. self. startactivity (mintent );}}). setneutralbutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int whichbutton) {dialog. cancel ();}}). create (); B. show ();} toast. maketext (androidbaseactivity. self, "test", 1 ). show (); Return flag ;}
The code in this part is mainly used in two places:
One is: connectivitymanager cwjmanager = (connectivitymanager) androidbaseactivity. Self. getsystemservice
(Context. connectivity_service );
The other is: cwjmanager. getactivenetworkinfo (). isavailable ();
The two pieces of code are the same as those in the first part. They only use isavailable () to check whether the network is active. However, for configurations that require connection to servers such as WiFi, it is worth thinking about whether a network is available.
My final understanding of these three parts is that the connectivitymanager of Android manages all the networks in a unified manner (a bit idiotic .....).
IV,Android programming settings (source:Http://www.oschina.net/code/snippet_12_212)
Contentvalues values = new contentvalues (); values. put (name, "CMCC cmwap"); values. put (APN, "cmwap"); values. put (proxy, "10.0.0.172"); values. put (port, "80"); values. put (mmsproxy, ""); values. put (mmsport, ""); values. put (user, ""); values. put (server, ""); values. put (password, ""); values. put (mmsc, ""); values. put (type, ""); values. put (MCC, "460"); values. put (MNC, "00"); values. put (numeric, "46000"); reuri = getcontentresolver (). insert (URI. parse ("content: // telephony/carriers"), values); // The preferred access point "content: // telephony/carriers/preferapn"
5. Enable and disable WiFi
Android. permission. access_wifi_state android. permission. change_wifi_state android. permission. wake_lock2. Obtain wifimanagerwifimanager = (wifimanager) This. getsystemservice (context. wifi_service); 3. enable or disable wifiif (wifimanager. iswifienabled () {wifimanager. setwifienabled (false);} else {wifimanager. setwifienabled (true );}
6. Determine whether it is WIFI or 3G network. The user's embodiment is here. WiFi can be downloaded or played online. Http://www.cnblogs.com/codeworker/archive/2012/04/23/2467180.html)
public static boolean isWifi(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkINfo = cm.getActiveNetworkInfo(); if (networkINfo != null && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } return false; }