读取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;
}};
}