经验来自学习之应用网络处理
开发移动软件处理网络连接对于程序员是一个头疼的问题,至少我这个菜鸟这么认为,翻了几个软件发现有几个软件在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 DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce 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 DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce 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网络
补充:移动开发 , Android ,