讀取Android設備的MAC地址
AndroidMACTask.java
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;
}};
}