@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();
}
}
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(); } }
< !-- 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);