States.java 2.2 KB
Newer Older
勇敢di牛牛's avatar
勇敢di牛牛 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
package com.example.testapp.Util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import android.net.NetworkInfo.State;
import android.util.Log;

public class States {
    private static int Net_State_Wifi = 2;
    private static int Net_State_Mobile = 1;
    private static int Net_No = 0;
    private static int Net_others = 3;
    private static int Get_Net_State_Fail = -1;
    private static int Net_State = Get_Net_State_Fail;
    private static String TAG = "States";
    public static void isNetworkConnected(Context context) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        Log.i(TAG, String.format("isNetworkConnected: %s",mNetworkInfo));
        if (null == mNetworkInfo){
            Net_State = Get_Net_State_Fail;
            return;
        }

        if (mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE){
            Log.i(TAG, String.format("isNetworkConnected: %s",mNetworkInfo.getType()));
            Net_State = Net_State_Mobile;
            return;
        }
        if (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI){
            Log.i(TAG, String.format("isNetworkConnected: %s",mNetworkInfo.isConnected()));
            Net_State = Net_State_Wifi;
            return;
        }
        if (!mNetworkInfo.isConnected()){
            Log.i(TAG, String.format("isNetworkConnected: %s",mNetworkInfo.isConnected()));
            Net_State = Net_No;
        }else{
            Log.i(TAG, String.format("isNetworkConnected: %s",mNetworkInfo.isConnected()));
            Net_State = Net_others;
        }
    }

    public static String getNetName(Context context){
        isNetworkConnected(context);
        if (Net_State == Net_No){
            return "Net_No";
        }else if(Net_State == Net_State_Mobile){
            return "Net_State_Mobile";
        }else if(Net_State == Net_State_Wifi){
            return "Net_State_Wifi";
        }else if(Net_State == Get_Net_State_Fail){
            return "Get_Net_State_Fail";
        }else{
            return "Net_others";
        }
    }
勇敢di牛牛's avatar
勇敢di牛牛 已提交
60

勇敢di牛牛's avatar
勇敢di牛牛 已提交
61
}