---
title: "Android连接网络问题"
author: "263229365"
date: "2012-11-23T11:14:41Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-11-23T11:14:41Z"
id: 1733853
url: "https://263229365.iteye.com/blog/1733853"
---

一、检查网络是否连接

 

方法一、

 

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;

   //如果不可用,弹出对话框

 }

 

方法二：

 

`public` `class` `ConnectionDetector {`

`08` ` ` 

`09` `private` `Context _context;`

`10` ` ` 

`11` `public` `ConnectionDetector(Context context){`

`12` `this._context = context;`

`13` `}`

`14` ` ` 

`15` `public` `boolean` `isConnectingToInternet(){`

`16` `ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);`

`17` `if` `(connectivity != null)`

`18` `{`

`19` `NetworkInfo[] info = connectivity.getAllNetworkInfo();`

`20` `if` `(info != null)`

`21` `for` `(int` `i = 0; i < info.length; i++)`

`22` `if` `(info[i].getState() == NetworkInfo.State.CONNECTED)`

`23` `{`

`24` `return` `true;`

`25` `}`

`26` ` ` 

`27` `}`

`28` `return` `false;`

`29` `}`

`30` `}`

 

 

方法三、

 

```
1. 首先先看一下Android 判断网络是否已打开(如果直接写在Activity里面,则可以不需要参数)



    /*
     * 判断网络连接是否已开
     * 2012-08-20
     *true 已打开  false 未打开
     * */
    public static boolean isConn(Context context){
        boolean bisConnFlag=false;
        ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = conManager.getActiveNetworkInfo();
        if(network!=null){
            bisConnFlag=conManager.getActiveNetworkInfo().isAvailable();
        }
        return bisConnFlag;
    }



 
  2. 如果未开启网络调用打开设置界面(如果不写在Activity里面则不需要参数)




    /*
     * 打开设置网络界面
     * */
    public static void setNetworkMethod(final Context context){
        //提示对话框
        AlertDialog.Builder builder=new Builder(context);
        builder.setTitle("网络设置提示").setMessage("网络连接不可用,是否进行设置?").setPositiveButton("设置", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Intent intent=null;
                //判断手机系统的版本  即API大于10 就是3.0或以上版本 
                if(android.os.Build.VERSION.SDK_INT>10){
                    intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                }else{
                    intent = new Intent();
                    ComponentName component = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");
                    intent.setComponent(component);
                    intent.setAction("android.intent.action.VIEW");
                }
                context.startActivity(intent);
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        }).show();
    }



3.  在 AndroidManifest.xml中设置权限


    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

4. 在相应的位置调用即可


if(!ConnectionUtil.isConn(getApplicationContext())){
            ConnectionUtil.setNetworkMethod(MainActivity.this);
        }

```