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

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
<?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

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
60
61
62
63
64
65
66
67
68
69
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 外网  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

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
<?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

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
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 @   CrushGirl  阅读(1455)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示