開發移動軟件處理網絡連接對於程序員是一個頭疼的問題,至少我這個菜鳥這麼認為,翻了幾個軟件發現有幾個軟件在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);