【Tech-Android-Other】Android中的网络类型获取与处理
**Android中的网络类型获取与处理**
开发移动软件处理网络连接对于程序员是一个头疼的问题,至少我这个菜鸟这么认为,翻了几个软件发现有几个软件在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();
}
}
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>
使用方式一:
一、 在Activity的onCreate中:
//注册网络监听
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver, filter);
二、在Activity中的onDestroy中:
//取消监听
unregisterReceiver(mNetworkStateReceiver);
使用方式二:
一、应用启动时,启动Service,在Service的onCreate方法中注册网络监听:
//注册网络监听
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver, filter);
二、 应用退出时,Service关闭,在Service的onDestroy方法中取消监听:
//取消监听
unregisterReceiver(mNetworkStateReceiver);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。