Netstate检查网络是否连接

添加网络权限android:name=”android.permission.ACCESS_NETWORK_STATE”>

MainActivity

package com.wzq.netstatedemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int netType = NetStateUtil.getNetType(MainActivity.this);
        if (netType != -1) {
            initdata();
        }else {
            Toast.makeText(this, "请检查网络", Toast.LENGTH_SHORT).show();
        }


    }

    private void initdata() {
        Toast.makeText(this, "我有网络哈哈哈哈", Toast.LENGTH_SHORT).show();
    }
}

NetStateUtil

package com.wzq.netstatedemo;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * author:Created by WangZhiQiang on 2017-11-01.
 */

class NetStateUtil {

    /**
     * 获取手机网络状态
     * -1 :无网络
     *  0 :  蜂窝网络;
     * 1  : wifi网络
     * @return
     * @param context 上下文对象
     */
    public static int getNetType(Context context) {
        int mState = -1;// 代表无网络
        //获取android系统提供的服务, 转换成链接管理类,这个类专门处理链接相关的东西
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //NetworkInfo封装了网络链接的信息
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if(activeNetworkInfo ==null){
            return mState;
        }
        int type = activeNetworkInfo.getType();

        if (type == ConnectivityManager.TYPE_WIFI) {//代表现在是wifi网络
            mState = 1;
        }else if(type == ConnectivityManager.TYPE_MOBILE ){
            mState = 0;
        }
        return mState;
    }
}

你可能感兴趣的:(Netstate检查网络是否连接)