APK:ethernet广播监听以太网状态、ping外网、广播监听wifi的状态

 一.LAN/WAN/WLAN/WWAN区别总结

LAN: Local Area Network : 局域网
WAN: Wide Area Network, 广域网
WLAN: Wireless LAN, 无线局域网
WWAN: Wireless WAN: 无线广域网

二.以太网监听广播

2.1.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gatsby.networktest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="22"
        android:targetSdkVersion="22" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

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

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

        <receiver android:name=".MyBroadcast" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.net.wifi.STATE_CHANGE" />
                <action android:name="android.Net.wifi.WIFI_STATE_CHANGED" />
                <!-- 网络更改广播 -->
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

        <service android:name=".NetworkService" >
        </service>
    </application>

</manifest>  

2.2.MyBroadcast.java

package com.gatsby.networktest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class MyBroadcast extends BroadcastReceiver {

	static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
	public int NET_ETHERNET = 1;
	public int NET_WIFI = 2;
	public int NET_NOCONNECT = 0;

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub

		String action = intent.getAction();

		if (action.equals(BOOT_COMPLETED)) {
			Intent startService = new Intent(context, NetworkService.class);
			Log.d("gatsby", "BOOT_COMPLETED Broadcast-----------!!!!");
			context.startService(startService);
		} else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)
				|| action.equals("android.net.conn.CONNECTIVITY_CHANGE")) {

			switch (isNetworkAvailable(context)) {
			case 1:
				Log.d("gatsby", "-----------networktest---------ethernetState");
				Intent etherIntent = new Intent();
				etherIntent.setAction("com.xinhua.etherIntent");
				context.sendBroadcast(etherIntent);

				break;
			case 2:
				Log.d("gatsby", "-----------networktest---------wifinetState");
				Intent wifiIntent = new Intent();
				wifiIntent.setAction("com.xinhua.wifiIntent");
				context.sendBroadcast(wifiIntent);
				break;
			case 0:
				Log.d("gatsby", "-----------networktest---------NONO");

				break;
			default:
				break;
			}
		}

	}

	private int isNetworkAvailable(Context mcontext) {
		ConnectivityManager connectMgr = (ConnectivityManager) mcontext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ethNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
		NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		if (ethNetInfo != null && ethNetInfo.isConnected()) {
			return NET_ETHERNET;
		} else if (wifiNetInfo != null && wifiNetInfo.isConnected()) {
			return NET_WIFI;
		} else {
			return NET_NOCONNECT;
		}
	}

}

 三.节点判断以太网插拔状态   cat /sys/class/net/eth0/carrier

四.ping 外网  

	public static final boolean ping() {

		String result = null;
		try {
			String ip = "www.baidu.com";
			Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);
			int status = p.waitFor();
			if (status == 0) {
				result = "success";
				return true;
			} else {
				result = "failed";
			}
		} catch (IOException e) {
			result = "IOException";
		} catch (InterruptedException e) {
			result = "InterruptedException";
		} finally {
			Log.d("gatsby", " ping result = " + result);
		}
		return false;
	}

 五.wifi网卡状态、操作网卡的权限

5.1.wifi网卡状态
a.WIFI_STATE_DISABLED:WIFI网卡不可用
b.WIFI_STATE_DISABLING:WIFI正在关闭
c.WIFI_STATE_ENABLED:WIFI网卡可用
d.WIFI_STATE_ENABLING:WIFI网卡正在打开
e.WIFI_STATE_UNKNOWN:未知网卡状态

 5.2.demo

a.AndroidManifest.xml

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

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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" />
                <data
                    android:host="akm.app"
                    android:pathPrefix="/openwith"
                    android:scheme="myapp" />
            </intent-filter>
        </activity>

        <receiver android:name=".MyBroadcast">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.wifi.STATE_CHANGE" />
            </intent-filter>
        </receiver>

    </application>

</manifest>  

b.MyBroadcast

package com.gatsby.wifienable;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;

public class MyBroadcast extends BroadcastReceiver {

    Context mContext;
    WifiManager mWifiManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        this.mContext = context;
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            Log.d("gatsby", "wifiState->" + wifiState);

            //wifi 开关
            mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            mWifiManager.setWifiEnabled(false);

            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                    Log.d("gatsby", " WIFI_STATE_DISABLED " + wifiState);
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    Log.d("gatsby", " WIFI_STATE_DISABLING " + wifiState);
                    break;
                case WifiManager.WIFI_STATE_ENABLED:
                    Log.d("gatsby", " WIFI_STATE_ENABLED " + wifiState);
                    break;
                case WifiManager.WIFI_STATE_ENABLING:
                    Log.d("gatsby", " WIFI_STATE_ENABLING " + wifiState);
                    break;
                case WifiManager.WIFI_STATE_UNKNOWN:
                    Log.d("gatsby", " WIFI_STATE_UNKNOWN " + wifiState);
                    break;
            }
        }
    }
}

  

posted @ 2020-08-10 14:01  CrushGirl  阅读(1415)  评论(0编辑  收藏  举报