【问题标题】:how can I Get MAC ADDRESS android 7.0我怎样才能获得MAC地址android 7.0
【发布时间】:2017-12-02 12:01:05
【问题描述】:

我无法获取mac地址 谁能帮帮我 我是android开发的新手,请帮助我 manifest.xml

    <receiver
        android:name="com.example.v3rlu.authentification.DeviceAdmin"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data android:name="android.app.device_admin"
            android:resource="@xml/xml" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
            <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
        </intent-filter>
    </receiver>

启动活动检查

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check);
    mac=(TextView) findViewById(R.id.textView) ;
    DeviceAdminReceiver admin = new DeviceAdminReceiver();
    DevicePolicyManager devicepolicymanager = 
    admin.getManager(getApplicationContext());
    ComponentName name1 = admin.getWho(getApplicationContext());
    if (devicepolicymanager.isAdminActive(name1)){
        mac_address = devicepolicymanager.getWifiMacAddress(name1);}

mac.setText(mac_address);
}}

对于 receiverclass 我从 android 网站获得了代码

【问题讨论】:

标签: android xml


【解决方案1】:

试试这个。

public String getMacAddr() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }

            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                //res1.append(Integer.toHexString(b & 0xFF) + ":");
                res1.append(String.format("%02X:",b));
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
    }
    return "";
}

在清单中添加互联网权限

<uses-permission android:name="android.permission.INTERNET"/>

【讨论】:

    【解决方案2】:

    尝试使用 WifiManager 获取 mac 地址。试试下面的代码。

    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wInfo = wifiManager.getConnectionInfo();
    String macAddress = wInfo.getMacAddress(); 
    

    在清单中添加权限

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 2017-05-16
      • 2011-06-04
      • 1970-01-01
      • 2017-12-18
      • 2012-04-08
      • 2017-05-25
      相关资源
      最近更新 更多