Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 讀取Android設備的MAC地址

讀取Android設備的MAC地址

編輯:關於Android編程

AndroidMACTask.java


[java]
<SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 12px">package com.csdn.android.util; 
 
import android.os.AsyncTask; 
import android.os.AsyncTask.Status; 
import com.csdn.android.datamanage.module.LicenseCheck; 
 
public class AndroidMACTask  

    private static boolean mInit=false; 
 
    public static void checkDeviceMac(boolean isOk) 
    { 
        if(mInit) 
        { 
            return;  
        } 
        mInit=true; 
        //獲取失敗,則進入子線程重新獲取  
        if(!isOk) 
        { 
            //初始化Mac地址  
            if(mInitMacTask==null || mInitMacTask.getStatus() == Status.RUNNING ||mInitMacTask.getStatus() == Status.FINISHED) 
            { 
                return; 
            } 
            mInitMacTask.execute(); 
        } 
        //獲取成功,並且從緩存獲取  
        else if(AndroidUtil.isReaderMACCache()) 
        { 
            //讀取設置Mac地址  
            if(mReaderMacFromDeviceTask==null || mReaderMacFromDeviceTask.getStatus() == Status.RUNNING ||mReaderMacFromDeviceTask.getStatus() == Status.FINISHED) 
            { 
                return; 
            } 
            mReaderMacFromDeviceTask.execute(); 
        } 
    } 
     
    //讀取MAC地址任務  
        private static AsyncTask<Void, Void, Void> mInitMacTask = new AsyncTask<Void, Void, Void>(){ 
 
            @Override 
            protected Void doInBackground(Void... params)  
            { 
                boolean isOk = AndroidUtil.initMACdoInBackground(); 
                //假如獲取MAC成功,並且設備是通過MAC授權,則重新初始化授權  
                if(isOk && AndroidUtil.isReaderMac()) 
                { 
                    LicenseCheck.updateCacheState(FrameHelper.getAppContext(), false); 
                } 
                return null; 
            }}; 
         
        //讀取MAC地址任務  
        private static AsyncTask<Void, Void, Void> mReaderMacFromDeviceTask = new AsyncTask<Void, Void, Void>(){ 
 
            @Override 
            protected Void doInBackground(Void... params)  
            { 
                String mac = AndroidUtil.getMacFromDeviceInBackground(); 
                //假如設備不是通過MAC授權的,則把物理地址放到緩存  
                if(!AndroidUtil.isReaderMac()) 
                { 
                    AndroidUtil.saveMacInfo(mac); 
                    return null; 
                } 
                //MAC緩存地址同物理地址  
                if(AndroidUtil.getMAC().equals(mac)) 
                { 
                    AndroidUtil.saveMacInfo(mac); 
                    return null; 
                } 
                //MAC物理地址不同與緩存地址,重新初始化  
                AndroidUtil.saveMacInfo(mac); 
                LicenseCheck.updateCacheState(FrameHelper.getAppContext(), false); 
                return null; 
            }}; 
             

</SPAN> 

package com.csdn.android.util;

import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import com.csdn.android.datamanage.module.LicenseCheck;

public class AndroidMACTask
{
 private static boolean mInit=false;

 public static void checkDeviceMac(boolean isOk)
 {
  if(mInit)
  {
   return;
  }
  mInit=true;
  //獲取失敗,則進入子線程重新獲取
  if(!isOk)
  {
   //初始化Mac地址
   if(mInitMacTask==null || mInitMacTask.getStatus() == Status.RUNNING ||mInitMacTask.getStatus() == Status.FINISHED)
   {
    return;
   }
   mInitMacTask.execute();
  }
  //獲取成功,並且從緩存獲取
  else if(AndroidUtil.isReaderMACCache())
  {
   //讀取設置Mac地址
   if(mReaderMacFromDeviceTask==null || mReaderMacFromDeviceTask.getStatus() == Status.RUNNING ||mReaderMacFromDeviceTask.getStatus() == Status.FINISHED)
   {
    return;
   }
   mReaderMacFromDeviceTask.execute();
  }
 }
 
 //讀取MAC地址任務
  private static AsyncTask<Void, Void, Void> mInitMacTask = new AsyncTask<Void, Void, Void>(){

   @Override
   protected Void doInBackground(Void... params)
   {
    boolean isOk = AndroidUtil.initMACdoInBackground();
    //假如獲取MAC成功,並且設備是通過MAC授權,則重新初始化授權
    if(isOk && AndroidUtil.isReaderMac())
    {
     LicenseCheck.updateCacheState(FrameHelper.getAppContext(), false);
    }
    return null;
   }};
  
  //讀取MAC地址任務
  private static AsyncTask<Void, Void, Void> mReaderMacFromDeviceTask = new AsyncTask<Void, Void, Void>(){

   @Override
   protected Void doInBackground(Void... params)
   {
    String mac = AndroidUtil.getMacFromDeviceInBackground();
    //假如設備不是通過MAC授權的,則把物理地址放到緩存
    if(!AndroidUtil.isReaderMac())
    {
     AndroidUtil.saveMacInfo(mac);
     return null;
    }
    //MAC緩存地址同物理地址
    if(AndroidUtil.getMAC().equals(mac))
    {
     AndroidUtil.saveMacInfo(mac);
     return null;
    }
    //MAC物理地址不同與緩存地址,重新初始化
    AndroidUtil.saveMacInfo(mac);
    LicenseCheck.updateCacheState(FrameHelper.getAppContext(), false);
    return null;
   }};
   
}
AndroidUtil.java


[java] view plaincopyprint?<SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 12px">package com.csdn.android.util; 
 
import com.csdn.android.framework.util.StringUtil; 
import android.content.Context; 
import android.net.wifi.WifiInfo; 
import android.net.wifi.WifiManager; 
import android.telephony.TelephonyManager; 
 
public class AndroidUtil  

    //是否允許MAC使用MAC地址  
    private static boolean ALLOW_MAC=true; 
    //唯一標識的長度  
    private static int IDENTIFY_LEN=14; 
    //MAC地址的標識  
    private static String KEY_MAC="android_mac";     
    //初始化IMEI  
    private static boolean mIntiIMEI = false; 
    //初始化MAC  
    private static boolean mInitMAC=false; 
    //IMEI緩存  
    private static String mIMEI; 
    //MAC地址緩存  
    private static String mMAC; 
    //是否讀取緩存  
    private static boolean mReaderCache=false; 
     
    //是否讀取的MAC地址  
    public static boolean isReaderMac() 
    { 
        //沒有完成  
        if(!isInitCompeleted()) 
        { 
            return false; 
        } 
        //不允許MAC地址  
        if(!ALLOW_MAC) 
        { 
            return false; 
        } 
        //取IMEI地址  
        if(!StringUtil.isNull(getIMEI())) 
        { 
            return false; 
        } 
        //取MAC地址  
        return true; 
    } 
     
    //判斷是否讀取緩存  
    public static boolean isReaderMACCache() 
    { 
        return mReaderCache; 
    } 
     
    //獲取IMEI地址  
    private static String getIMEI() { 
        if (mIntiIMEI) { 
            return mIMEI; 
        } 
        TelephonyManager telephonyManager = (TelephonyManager) FrameHelper 
                .getAppContext().getSystemService(Context.TELEPHONY_SERVICE); 
        try { 
            mIMEI = telephonyManager.getDeviceId(); 
        } catch (Exception e) { 
        } 
        mIntiIMEI = true; 
        return mIMEI; 
    } 
     
    //是否初始化完成  
    public static boolean isInitCompeleted() 
    { 
        if(!StringUtil.isNull(getIMEI()) || mInitMAC) 
        { 
            return true; 
        } 
        return false; 
    } 
     
    /**
     * 獲取設備唯一標識,IMEI 或者 MAC
     * @return
     */ 
    public static String getIdentifyId() 
    { 
        //沒有完成  
        if(!isInitCompeleted()) 
        { 
            return null; 
        } 
        //不允許MAC地址  
        if(!ALLOW_MAC) 
        { 
            return getIMEI(); 
        } 
        //取IMEI地址  
        if(!StringUtil.isNull(getIMEI())) 
        { 
            return getIMEI(); 
        } 
        //取MAC地址  
        return getMAC(); 
    } 
     
    //獲取MAC地址  
    public static String getMAC()  
    { 
        if (mInitMAC)  
        { 
            return mMAC; 
        } 
        return null; 
    } 
     
    //獲取真實MAC地址  
    public static String getMACSource()  
    { 
        if (mInitMAC && !StringUtil.isNull(mMAC))  
        { 
            if(mMAC.length()>12) 
            { 
                return mMAC.substring(0, 12); 
            } 
            return mMAC; 
        } 
        return ""; 
    } 
     
    public static boolean initMACInMainThread() 
    { 
        return initMAC(1,true); 
    } 
     
    public static   boolean initMACdoInBackground() 
    { 
        //嘗試100次  
        return initMAC(100,false); 
    } 
     
    //後台獲取MAC地址  
    public static String getMacFromDeviceInBackground() 
    { 
        return getMacFromDevice(100); 
    } 
     
    private static boolean initMAC(int internal,boolean readerCache) 
    { 
        String mac = null; 
        //是否讀取緩存  
        if(readerCache) 
        { 
            //通過緩存獲取  
            mac = FrameHelper.readSharedData(FrameHelper.getAppContext(), KEY_MAC); 
            if(!StringUtil.isNull(mac)) 
            { 
                mInitMAC = true; 
                mMAC = mac; 
                mReaderCache =true; 
                return true; 
            } 
        } 
         
        mac = getMacFromDevice(internal); 
        if(!StringUtil.isNull(mac)) 
        { 
            saveMacInfo(mac); 
            mReaderCache = false; 
            return true; 
        } 
        return false; 
    } 
     
    //保存Mac地址  
    public static void saveMacInfo(String mac) 
    { 
        mInitMAC = true; 
        mMAC = mac; 
        FrameHelper.saveSharedData(FrameHelper.getAppContext(), KEY_MAC,mMAC); 
    } 
     
    //嘗試讀取MAC地址  
    private static String getMacFromDevice(int internal) 
    { 
        String mac=null; 
         
        WifiManager wifiManager = (WifiManager) FrameHelper.getAppContext().getSystemService(Context.WIFI_SERVICE);      
        //嘗試獲取mac  
        mac = tryGetMAC(wifiManager); 
        if(!StringUtil.isNull(mac)) 
        { 
            return mac; 
        } 
         
        //獲取失敗,嘗試打開wifi獲取  
        boolean isOkWifi = tryOpenMAC(wifiManager); 
        for(int index=0;index<internal;index++) 
        { 
            //如果第一次沒有成功,第二次做100毫秒的延遲。  
            if(index!=0) 
            { 
                try  
                { 
                    Thread.sleep(100); 
                }  
                catch (InterruptedException e)  
                { 
                    e.printStackTrace(); 
                } 
            }            
            mac = tryGetMAC(wifiManager); 
            if(!StringUtil.isNull(mac)) 
            { 
 
                break; 
            } 
        } 
         
        //嘗試關閉wifi  
        if(isOkWifi) 
        { 
            tryCloseMAC(wifiManager); 
        }    
        return mac; 
    } 
     
    //嘗試打開MAC  
    private static boolean tryOpenMAC(WifiManager manager) 
    { 
        boolean softOpenWifi = false; 
        int state = manager.getWifiState(); 
        if (state != WifiManager.WIFI_STATE_ENABLED     && state != WifiManager.WIFI_STATE_ENABLING)  
        { 
            manager.setWifiEnabled(true); 
            softOpenWifi = true; 
        } 
        return softOpenWifi; 
    } 
     
    //嘗試關閉MAC  
    private static void tryCloseMAC(WifiManager manager) 
    { 
        manager.setWifiEnabled(false); 
    } 
     
    //嘗試獲取MAC地址  
    private static String tryGetMAC(WifiManager manager) 
    { 
        WifiInfo wifiInfo = manager.getConnectionInfo(); 
        if (wifiInfo == null || StringUtil.isNull(wifiInfo.getMacAddress()))  
        { 
            return null; 
        } 
        String mac = wifiInfo.getMacAddress().replaceAll(":", "").trim().toUpperCase(); 
        mac = formatIdentify(mac); 
        return mac; 
    } 
     
    //格式化唯一標識  
    private static String formatIdentify(String identify) 
    { 
        //判空  
        if(StringUtil.isNull(identify)) 
        { 
            return identify; 
        } 
        //去除空格  
        identify = identify.trim(); 
        //求長度  
        int len = identify.length(); 
        //正好  
        if(len== IDENTIFY_LEN) 
        { 
            return identify; 
        } 
        //過長,截取  
        if(len>IDENTIFY_LEN) 
        { 
            return identify.substring(0, IDENTIFY_LEN); 
        } 
        //過短,補0  
        for(;len<IDENTIFY_LEN;len++) 
        { 
            identify += "0"; 
        } 
        //大於默認  
        return identify;         
    } 

</SPAN> 

package com.csdn.android.util;

import com.csdn.android.framework.util.StringUtil;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

public class AndroidUtil
{
 //是否允許MAC使用MAC地址
 private static boolean ALLOW_MAC=true;
 //唯一標識的長度
 private static int IDENTIFY_LEN=14;
 //MAC地址的標識
 private static String KEY_MAC="android_mac"; 
 //初始化IMEI
 private static boolean mIntiIMEI = false;
 //初始化MAC
 private static boolean mInitMAC=false;
 //IMEI緩存
 private static String mIMEI;
 //MAC地址緩存
 private static String mMAC;
 //是否讀取緩存
 private static boolean mReaderCache=false;
 
 //是否讀取的MAC地址
 public static boolean isReaderMac()
 {
  //沒有完成
  if(!isInitCompeleted())
  {
   return false;
  }
  //不允許MAC地址
  if(!ALLOW_MAC)
  {
   return false;
  }
  //取IMEI地址
  if(!StringUtil.isNull(getIMEI()))
  {
   return false;
  }
  //取MAC地址
  return true;
 }
 
 //判斷是否讀取緩存
 public static boolean isReaderMACCache()
 {
  return mReaderCache;
 }
 
 //獲取IMEI地址
 private static String getIMEI() {
  if (mIntiIMEI) {
   return mIMEI;
  }
  TelephonyManager telephonyManager = (TelephonyManager) FrameHelper
    .getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
  try {
   mIMEI = telephonyManager.getDeviceId();
  } catch (Exception e) {
  }
  mIntiIMEI = true;
  return mIMEI;
 }
 
 //是否初始化完成
 public static boolean isInitCompeleted()
 {
  if(!StringUtil.isNull(getIMEI()) || mInitMAC)
  {
   return true;
  }
  return false;
 }
 
 /**
  * 獲取設備唯一標識,IMEI 或者 MAC
  * @return
  */
 public static String getIdentifyId()
 {
  //沒有完成
  if(!isInitCompeleted())
  {
   return null;
  }
  //不允許MAC地址
  if(!ALLOW_MAC)
  {
   return getIMEI();
  }
  //取IMEI地址
  if(!StringUtil.isNull(getIMEI()))
  {
   return getIMEI();
  }
  //取MAC地址
  return getMAC();
 }
 
 //獲取MAC地址
 public static String getMAC()
 {
  if (mInitMAC)
  {
   return mMAC;
  }
  return null;
 }
 
 //獲取真實MAC地址
 public static String getMACSource()
 {
  if (mInitMAC && !StringUtil.isNull(mMAC))
  {
   if(mMAC.length()>12)
   {
    return mMAC.substring(0, 12);
   }
   return mMAC;
  }
  return "";
 }
 
 public static boolean initMACInMainThread()
 {
  return initMAC(1,true);
 }
 
 public static  boolean initMACdoInBackground()
 {
  //嘗試100次
  return initMAC(100,false);
 }
 
 //後台獲取MAC地址
 public static String getMacFromDeviceInBackground()
 {
  return getMacFromDevice(100);
 }
 
 private static boolean initMAC(int internal,boolean readerCache)
 {
  String mac = null;
  //是否讀取緩存
  if(readerCache)
  {
   //通過緩存獲取
   mac = FrameHelper.readSharedData(FrameHelper.getAppContext(), KEY_MAC);
   if(!StringUtil.isNull(mac))
   {
    mInitMAC = true;
    mMAC = mac;
    mReaderCache =true;
    return true;
   }
  }
  
  mac = getMacFromDevice(internal);
  if(!StringUtil.isNull(mac))
  {
   saveMacInfo(mac);
   mReaderCache = false;
   return true;
  }
  return false;
 }
 
 //保存Mac地址
 public static void saveMacInfo(String mac)
 {
  mInitMAC = true;
  mMAC = mac;
  FrameHelper.saveSharedData(FrameHelper.getAppContext(), KEY_MAC,mMAC);
 }
 
 //嘗試讀取MAC地址
 private static String getMacFromDevice(int internal)
 {
  String mac=null;
  
  WifiManager wifiManager = (WifiManager) FrameHelper.getAppContext().getSystemService(Context.WIFI_SERVICE);  
  //嘗試獲取mac
  mac = tryGetMAC(wifiManager);
  if(!StringUtil.isNull(mac))
  {
   return mac;
  }
  
  //獲取失敗,嘗試打開wifi獲取
  boolean isOkWifi = tryOpenMAC(wifiManager);
  for(int index=0;index<internal;index++)
  {
   //如果第一次沒有成功,第二次做100毫秒的延遲。
   if(index!=0)
   {
    try
    {
     Thread.sleep(100);
    }
    catch (InterruptedException e)
    {
     e.printStackTrace();
    }
   }   
   mac = tryGetMAC(wifiManager);
   if(!StringUtil.isNull(mac))
   {

    break;
   }
  }
  
  //嘗試關閉wifi
  if(isOkWifi)
  {
   tryCloseMAC(wifiManager);
  } 
  return mac;
 }
 
 //嘗試打開MAC
 private static boolean tryOpenMAC(WifiManager manager)
 {
  boolean softOpenWifi = false;
  int state = manager.getWifiState();
  if (state != WifiManager.WIFI_STATE_ENABLED  && state != WifiManager.WIFI_STATE_ENABLING)
  {
   manager.setWifiEnabled(true);
   softOpenWifi = true;
  }
  return softOpenWifi;
 }
 
 //嘗試關閉MAC
 private static void tryCloseMAC(WifiManager manager)
 {
  manager.setWifiEnabled(false);
 }
 
 //嘗試獲取MAC地址
 private static String tryGetMAC(WifiManager manager)
 {
  WifiInfo wifiInfo = manager.getConnectionInfo();
  if (wifiInfo == null || StringUtil.isNull(wifiInfo.getMacAddress()))
  {
   return null;
  }
  String mac = wifiInfo.getMacAddress().replaceAll(":", "").trim().toUpperCase();
  mac = formatIdentify(mac);
  return mac;
 }
 
 //格式化唯一標識
 private static String formatIdentify(String identify)
 {
  //判空
  if(StringUtil.isNull(identify))
  {
   return identify;
  }
  //去除空格
  identify = identify.trim();
  //求長度
  int len = identify.length();
  //正好
  if(len== IDENTIFY_LEN)
  {
   return identify;
  }
  //過長,截取
  if(len>IDENTIFY_LEN)
  {
   return identify.substring(0, IDENTIFY_LEN);
  }
  //過短,補0
  for(;len<IDENTIFY_LEN;len++)
  {
   identify += "0";
  }
  //大於默認
  return identify;  
 }
}


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved