码迷,mamicode.com
首页 > 其他好文 > 详细

网络连接异常处理工具

时间:2016-03-28 16:49:30      阅读:124      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!