android网络状态判断(wifi、3G、2G)
熊孩纸
阅读:639
2021-04-01 10:04:56
评论:0
首先需要加入权限
代码如下 |
复制代码 |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.INTERNET"/> |
检查网络状态代码如下
代码如下 |
复制代码 |
public boolean CheckNetworkState() {
boolean flag = false; ConnectivityManager manager = (ConnectivityManager)getSystemService( Context.CONNECTIVITY_SERVICE); if(manager.getActiveNetworkInfo() != null) {
flag = manager.getActiveNetworkInfo().isAvailable(); } if(!flag) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.netstate); builder.setMessage(R.string.setnetwork); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub Intent mIntent = new Intent("/"); ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); mIntent.setComponent(comp); mIntent.setAction("android.intent.action.VIEW"); startActivity(mIntent); } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.cancel(); } }); builder.create(); builder.show(); } return flag; } |
效果如下,用户可以点击确定来设置网络,比如启动wifi
-------------------------------------------------------------------------------------------------
如果希望网络连接时做一些事情的话,需要判断当前网络状态是否为true,为真则进行一些操作,否则设置网络。还可以用另外一种方式,其实差不多
代码如下 |
复制代码 |
//setnetwork public void setNetwork() {
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.netstate); builder.setMessage(R.string.setnetwork); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub Intent mIntent = new Intent("/"); ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); mIntent.setComponent(comp); mIntent.setAction("android.intent.action.VIEW"); startActivity(mIntent); } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.cancel(); } }); builder.create(); builder.show(); } //NETWORK public boolean isNetworkAvailable() {
Context context = getApplicationContext(); ConnectivityManager connect = (ConnectivityManager)context.getSystemService( Context.CONNECTIVITY_SERVICE); if(connect==null) {
return false; }else//get all network info {
NetworkInfo[] info = connect.getAllNetworkInfo(); if(info!=null) {
for(int i=0;i<info.length;i++) {
if(info[i].getState()==NetworkInfo.State.CONNECTED) {
return true; } } } } return false; } |
然后通过判断如果当前状态可用则执行相关代码,不可用则设置网络
代码如下 |
复制代码 |
if(isNetworkAvailable()) {
相关代码 }else {
setNetwork(); }] |
这篇文章中已经介绍了网络状态的判断,但是测试后发现用此方法只能够判断WIFI是否连接,而现在的智能机可以是wifi、3G、甚至2G。那么如果用上面的方法就不行了需要进行一下修改,使其在网络不连接(无论wifi、3G、2G)的时候也可以判断。代码如下
代码如下 |
复制代码 |
//Check network public void CheckNetworkState() {
boolean flag = false; ConnectivityManager manager = (ConnectivityManager)getSystemService( Context.CONNECTIVITY_SERVICE); State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); //如果3G、wifi、2G等网络状态是连接的,则退出,否则显示提示信息进入网络设置界面 if(mobile == State.CONNECTED||mobile==State.CONNECTING) return; if(wifi == State.CONNECTED||wifi==State.CONNECTING) return; showTips(); } private void showTips() {
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.netstate); builder.setMessage(R.string.setnetwork); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
// 如果没有网络连接,则进入网络设置界面 startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.cancel(); } }); builder.create(); builder.show(); } |
种方式更加简单,也更加有用,当然第一篇也有可取之处,可以加入3G状态检查即可
注意:别忘了加上权限
代码如下 |
复制代码 |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> |