程序员人生 网站导航

判断网络连接并跳到设置界面

栏目:互联网时间:2014-10-12 13:57:29
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; }


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐