首页 > 其他 > 详细

网络连接异常处理工具

时间:2016-03-28 16:46:17      阅读:162      评论:0      收藏:0      [点我收藏+]
public class NetStateUtils {


/**
* 对网络连接状态进行判断
*
* @return true, 可用; false, 不可用
*/
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if (networkInfo != null) {
return networkInfo.isAvailable();
}
}
return false;
}


/**
* 提示设置网络连接
*
*/
public static void alertSetNetwork(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(context.getResources().getString(R.string.neterror)).setMessage(context.getString(R.string.question));

builder.setPositiveButton(context.getResources().getString(R.string.set), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = null;
try {
int sdkVersion = android.os.Build.VERSION.SDK_INT;
if (sdkVersion > 10) {
intent = new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS);
} else {
intent = new Intent();
ComponentName comp = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
}
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
});
builder.setNegativeButton(context.getString(R.string.cancle), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
System.exit(0);
System.gc();
}
});
builder.show();
}
}

网络连接异常处理工具

原文:http://www.cnblogs.com/zhou2016/p/5329495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!