Application network processing based on learning experience

Source: Internet
Author: User

Developing mobile software to process network connections is a headache for programmers. At least I think this cainiao thinks that after several software programs are turned over, several software found that the logo page is used to check whether the Network is available, on other pages, there is a broadcast for processing. After writing it all at once, paste the code and take a note @ Override protected void onResume () {// TODO Auto-generated method stub super. onResume (); if (checknetwork ()) {// code to jump to another page .......................} else {openNet ("network information", "cannot connect to the server, please check the network") ;}} private void openNet (String title, String message) {// ask the user to check the network AlertDialog. builder adb = new AlertD Ialog. builder (LogoActivity. this); final AlertDialog ad = adb. create (); // String current = "positioning information cannot be obtained currently"; ad. setTitle (title); ad. setMessage (message); ad. setButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// The above code is removed from the if (android. OS. build. VERSION. SDK_INT> 13) {// 3.2 or above to open the setting interface. You can also use ACTION_WIRELESS_SETTINGS to open it to wif. StartActivity (new Intent (android. provider. settings. ACTION_SETTINGS);} else {startActivity (new Intent (android. provider. settings. ACTION_WIRELESS_SETTINGS);} ad. dismiss () ;}}); ad. setButton2 ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {ad. dismiss (); System. exit (0) ;}}); ad. show ();} public boolean checknetwork () {Connectiv ItyManager mConnectivity = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE); TelephonyManager mTelephony = (TelephonyManager) LogoActivity. this. getSystemService (TELEPHONY_SERVICE); // check the network connection. If no network is available, NetworkInfo info = mConnectivity is not required. getActiveNetworkInfo (); if (info = null |! MConnectivity. getBackgroundDataSetting () {return false;} // determines the network connection type. Only 3G or Wi-Fi data is updated. Int netType = info. getType (); int netSubtype = info. getSubtype (); if (netType = ConnectivityManager. TYPE_WIFI) {return info. isConnected ();} else if (netType = ConnectivityManager. TYPE_MOBILE & netSubtype = TelephonyManager. NETWORK_TYPE_UMTS &&! MTelephony. isNetworkRoaming () {return info. isConnected () ;}else {return false ;}} network connection broadcast processing public class ConnectionChangeReceiver extends BroadcastReceiver {private static final String TAG = ConnectionChangeReceiver. class. getSimpleName (); @ Override public void onReceive (Context context, Intent intent) {Log. e (TAG, "network status change"); boolean success = false; // obtain the network connection service ConnectivityManager connManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); // State state = connManager. getActiveNetworkInfo (). getState (); // get the WIFI network connection State state = connManager. getNetworkInfo (ConnectivityManager. TYPE_WIFI ). getState (); // determines whether a Wi-Fi network is being used if (State. CONNECTED = state) {success = true;} // gets the GPRS network connection status state = connManager. getNetworkInfo (ConnectivityManager. TYPE_MOBILE ). getStat E (); // determine whether the GPRS network is being used if (State. CONNECTED! = State) {success = true;} if (! Success) {Toast. makeText (context, context. getString (R. string. your_network_has_disconnected), Toast. LENGTH_LONG ). show () ;}} [java] [java] [java] [java] public class ConnectionChangeReceiver extends BroadcastReceiver {private static final String TAG = ConnectionChangeReceiver. class. getSimpleName (); @ Override public void onReceive (Context context, Intent intent) {Log. e (TAG, "network status change"); boolean succe Ss = false; // obtain the network connection service ConnectivityManager connManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); // State state = connManager. getActiveNetworkInfo (). getState (); // get the WIFI network connection State state = connManager. getNetworkInfo (ConnectivityManager. TYPE_WIFI ). getState (); // determines whether a Wi-Fi network is being used if (State. CONNECTED = state) {success = true;} // gets the GPRS network connection status state = connManag Er. getNetworkInfo (ConnectivityManager. TYPE_MOBILE). getState (); // determines whether the GPRS network is being used if (State. CONNECTED! = State) {success = true;} if (! Success) {Toast. makeText (context, context. getString (R. string. your_network_has_disconnected), Toast. LENGTH_LONG ). show () ;}} remember to declare permissions in the Manifest file and register with the broadcast receiver. <! -- Needed to check when the network connection changes --> <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE "/> <receiver android: name =" you_package_name.ConnectionChangeReceiver "android: label =" NetworkConnection "> <intent-filter> <action android: name =" android.net. conn. CONNECTIVITY_CHANGE "/> </intent-filter> </receiver> usage Method 1: 1. in onCreate of Activity: // register network listener IntentFilter filter = new IntentFilter (); filter. addAction (ConnectivityManager. CONNECTIVITY_ACTION); registerReceiver (mNetworkStateReceiver, filter); 2. in onDestroy of Activity: // cancel listening for unregisterReceiver (mNetworkStateReceiver); usage Method 2: 1. when the application starts, start the Service and register the network listener in the onCreate method of the Service: // register the network listener IntentFilter filter = new IntentFilter (); filter. addAction (ConnectivityManager. CONNECTIVITY_ACTION); registerReceiver (mNetworkStateReceiver, filter); 2. when the application exits, the Service is disabled. Cancel listening in the onDestroy method of the Service: // cancel listening to unregisterReceiver (mNetworkStateReceiver );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.