開發移動軟體處理網路連接對於程式員是一個頭疼的問題,至少我這個菜鳥這麼認為,翻了幾個軟體發現有幾個軟體在logo頁面就是檢測網路是否可用,在其他頁面有一個broadcast去處理,寫了一下子,貼點代碼做個筆記@Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); if (checknetwork()) { //跳轉到其他頁面的代碼 ....................... }else { openNet("網路資訊", "無法串連上伺服器,請檢查網路"); } } private void openNet(String title, String message) { // 讓使用者檢查網路 AlertDialog.Builder adb = new AlertDialog.Builder( LogoActivity.this); final AlertDialog ad = adb.create(); // String current = "當前無法擷取定位資訊"; ad.setTitle(title); ad.setMessage(message); ad.setButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //由於4.0以上把原來的設定方式捨棄了所以上面的代碼捨去 if(android.os.Build.VERSION.SDK_INT > 13 ){ //3.2以上開啟設定介面,也可以直接用ACTION_WIRELESS_SETTINGS開啟到wifi介面 startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));} else { startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)); } ad.dismiss(); } }); ad.setButton2("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ad.dismiss(); System.exit(0); } }); ad.show(); } public boolean checknetwork() { ConnectivityManager mConnectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); TelephonyManager mTelephony = (TelephonyManager) LogoActivity.this .getSystemService(TELEPHONY_SERVICE); // 檢查網路連接,如果無網路可用,就不需要進行連網操作等 NetworkInfo info = mConnectivity.getActiveNetworkInfo(); if (info == null || !mConnectivity.getBackgroundDataSetting()) { return false; } // 判斷網路連接類型,只有在3G或wifi裡進行一些資料更新。 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; } } 網路連接廣播處理public class ConnectionChangeReceiver extends BroadcastReceiver { private static final String TAG =ConnectionChangeReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { Log.e(TAG, "網路狀態改變"); boolean success = false; //獲得網路連接服務 ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // State state = connManager.getActiveNetworkInfo().getState(); // 擷取WIFI網路連接狀態 State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); // 判斷是否正在使用WIFI網路 if (State.CONNECTED == state) { success = true; } // 擷取GPRS網路連接狀態 state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); // 判斷是否正在使用GPRS網路 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, "網路狀態改變"); boolean success = false; //獲得網路連接服務 ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // State state = connManager.getActiveNetworkInfo().getState(); // 擷取WIFI網路連接狀態 State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); // 判斷是否正在使用WIFI網路 if (State.CONNECTED == state) { success = true; } // 擷取GPRS網路連接狀態 state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); // 判斷是否正在使用GPRS網路 if (State.CONNECTED != state) { success = true; } if (!success) { Toast.makeText(context, context.getString(R.string.your_network_has_disconnected), Toast.LENGTH_LONG).show(); } } 記得在Manifest檔案裡面進行許可權聲明,和廣播接收器註冊。< !-- 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> 使用方式一:1. 在Activity的onCreate中://註冊網路監聽 IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mNetworkStateReceiver, filter); 2. 在Activity中的onDestroy中://取消監聽unregisterReceiver(mNetworkStateReceiver); 使用方式二:1. 應用啟動時,啟動Service,在Service的onCreate方法中註冊網路監聽://註冊網路監聽 IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mNetworkStateReceiver, filter); 2. 應用退出時,Service關閉,在Service的onDestroy方法中取消監聽://取消監聽unregisterReceiver(mNetworkStateReceiver);