Android开发-网络状态监测-AndroidStudio

转载请注明出处: http://blog.csdn.net/iwanghang/

觉得博文有用,请点赞,请评论,请关注,谢谢!~


网络可用、网络不可用;Wifi网络可用、Wifi网络不可用;Mobile网络可用、Mobile网络不可用;当前网络模式


老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:



MainActivity:

package com.iwanghang.netcheckdemo;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void checkNetClick(View view){
        boolean bool = isNetWorkConnected(this);
        if (bool){
            Toast.makeText(this, "网络可用", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, "网络不可用", Toast.LENGTH_SHORT).show();
        }
    }

    public void checkWifiClick(View view){
        boolean bool = isWifiConnected(this);
        if (bool){
            Toast.makeText(this, "Wifi网络可用", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, "Wifi网络不可用", Toast.LENGTH_SHORT).show();
        }
    }

    public void checkMobileClick(View view){
        boolean bool = isMobileConnected(this);
        if (bool){
            Toast.makeText(this, "Mobile网络可用", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, "Mobile网络不可用", Toast.LENGTH_SHORT).show();
        }
    }

    public void checkNetTypeClick(View view){
        int mType = getConnectedType(this);
        Toast.makeText(this, "当前网络模式为 = " + mType, Toast.LENGTH_SHORT).show();
    }

    // 判断网络是否可用
    public boolean isNetWorkConnected(Context context) {
        if (context!=null){
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo!=null){
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    // 判断WIFI网络是否可用
    public boolean isWifiConnected(Context context) {
        if (context!=null){
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mNetworkInfo!=null){
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    // 判断MOBILE网络是否可用
    public boolean isMobileConnected(Context context) {
        if (context!=null){
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mNetworkInfo!=null){
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    // 获得当前网络连接的类型信息
    public static int getConnectedType(Context context) {
        if (context!=null){
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo!=null && mNetworkInfo.isAvailable()){
                return mNetworkInfo.getType();
            }
        }
        return -1;
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iwanghang.netcheckdemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:text="检查网络"
        android:onClick="checkNetClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="59dp"
        android:id="@+id/button_checkNet" />

    <Button
        android:text="检查WIFI"
        android:onClick="checkWifiClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_checkNet"
        android:layout_alignLeft="@+id/button_checkNet"
        android:layout_alignStart="@+id/button_checkNet"
        android:id="@+id/button_checkWifi" />

    <Button
        android:text="检查移动网络"
        android:onClick="checkMobileClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_checkMobile"
        android:layout_below="@+id/button_checkWifi"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="检查网络模式"
        android:onClick="checkNetTypeClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_checkType"
        android:layout_below="@+id/button_checkMobile"
        android:layout_alignLeft="@+id/button_checkMobile"
        android:layout_alignStart="@+id/button_checkMobile" />

</RelativeLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iwanghang.netcheckdemo">

    <!-- 访问网络 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 访问网络状态 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




转载请注明出处: http://blog.csdn.net/iwanghang/



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值