在 你需要进行网络请求的页面的onCreate方法中 总之在网络请求之前 先判断有没有网络
checkNetwork(context);
if (!checkNetwork(context)) {
new Handler(context.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "请检查网络", Toast.LENGTH_LONG).show();
}
});
return;
}
项目问题我使用了Handler更新UI 你也可以不使用
添加下面的方法 到你的项目中就大功告成了 手动添加酷酷表情
private boolean checkNetwork(Context context) {
ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = conn.getActiveNetworkInfo();
if (net != null && net.isConnected()) {
return true;
}
return false;
}
本篇为转载博客 时间已经很久了 我已经忘了是哪位大神的博客 就不附上网址了 如有侵权 留言我会删除
如有不对的地方 欢迎大神指教