首页 > 代码库 > 判断网络连接并跳到设置界面
判断网络连接并跳到设置界面
if (isConnect(this)==false) { final AlertDialog dlg = new AlertDialog.Builder(this).create(); dlg.show(); Window window = dlg.getWindow(); window.setContentView(R.layout.shrew_exit_dialog); TextView title = (TextView) window.findViewById(R.id.title); title.setText("请开启网络连接!"); Button ok = (Button) window.findViewById(R.id.btn_ok); ok.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent("android.settings.WIRELESS_SETTINGS"); startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面 dlg.cancel(); } }); Button cancel = (Button) window.findViewById(R.id.btn_cancel); cancel.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { finish(); } }); }
public static boolean isConnect(Context context) { // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理) try { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { // 获取网络连接管理的对象 NetworkInfo info = connectivity.getActiveNetworkInfo(); if (info != null&& info.isConnected()) { // 判断当前网络是否已经连接 if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } catch (Exception e) { //TODO: handle exception Log.v("error",e.toString()); } return false; }
判断网络连接并跳到设置界面
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。