清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
用于判断软件打开时的网络连接状态,若无网络连接,提醒用户跳转到设置界面
需要注册的权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
/** * 设置在onStart()方法里面,可以在界面每次获得焦点的时候都进行检测 */ @Override protected void onStart() { ConnectivityManager manager = (ConnectivityManager) this .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null && info.isConnected()) { Toast.makeText(MainActivity.this, "网络连接连接", 0).show(); } else { AlertDialog.Builder builder = new Builder(MainActivity.this); builder.setTitle("开启网络服务"); builder.setMessage("网络没有连接,请到设置进行网络设置!"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (android.os.Build.VERSION.SDK_INT > 10) { // 3.0以上打开设置界面,也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面 startActivity(new Intent( android.provider.Settings.ACTION_SETTINGS)); } else { startActivity(new Intent( android.provider.Settings.ACTION_WIRELESS_SETTINGS)); } dialog.cancel(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } super.onStart(); }
需要注册的权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>