Android设备,由于生产厂家五花八门,各种标示不一定对所有的Android设备都起作用,手机和平板就有区别,为了能够准确的区分不同的Android设备,可以使用下面的类来唯一的标示一个Android设备:
UpLoadDeviceToken.java
package com.mxd.deviceToken;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
publicclassUpLoadDeviceToken
{
Context context;
publicUpLoadDeviceToken(Context context)
{
this.context =context;
}
/**
* @param context
* @return 将5种获得设备唯一标示的String拼接,并进行MD5加密
*/
@SuppressLint("DefaultLocale")
privateStringgetDeviceToken()
{
Stringm_szImei = getSZImei();
Stringm_szDevIDShort = getSzDevIDShort();
Stringm_szAndroidID = getAndroidID();
Stringm_szWLANMAC = getSzWLANMAC();
Stringm_szBTMAC = getSzBTMAC();
Stringm_szLongID =m_szImei +m_szDevIDShort + m_szAndroidID + m_szWLANMAC + m_szBTMAC;
MessageDigestm =null;// compute md5
try
{
m =MessageDigest.getInstance("MD5");
}catch (NoSuchAlgorithmExceptione)
{
e.printStackTrace();
}
m.update(m_szLongID.getBytes(),0,m_szLongID.length());
bytep_md5Data[] =m.digest();// get md5 bytes
Stringm_szUniqueID =new String();// create a hex string
for (inti =0; i <p_md5Data.length;i++)
{
intb = (0xFF &p_md5Data[i]);
if (b <=0xF)
m_szUniqueID +="0";
m_szUniqueID +=Integer.toHexString(b);
}
m_szUniqueID =m_szUniqueID.toUpperCase();
returnm_szUniqueID;
}
/**
* @param context
* @return 获取IMEI: 仅仅只对Android手机有效 需要的权限:READ_PHONE_STATE //
* <uses-permission
* android:name="android.permission.READ_PHONE_STATE" />
*/
privateStringgetSZImei()
{
StringszImei ="";
TelephonyManagerTelephonyMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (TelephonyMgr.getDeviceId() !=null && !TelephonyMgr.getDeviceId().equals(null))
{
szImei =TelephonyMgr.getDeviceId();// Requires READ_PHONE_STATE
}
returnszImei +"";
}
/**
* @return Pseudo-Unique ID, 这个在任何Android手机中都有效
*/
privateStringgetSzDevIDShort()
{
StringszDevIDShort ="35" +// we make this look like a valid IMEI
Build.BOARD.length() %10 +Build.BRAND.length() %10 +Build.CPU_ABI.length() %10 +Build.DEVICE.length() %10//
+Build.DISPLAY.length() %10 + Build.HOST.length() %10 +Build.ID.length() %10 +Build.MANUFACTURER.length() %10//
+Build.MODEL.length() %10 + Build.PRODUCT.length() %10 +Build.TAGS.length() %10 +Build.TYPE.length() %10//
+Build.USER.length() %10; // 13_digits
returnszDevIDShort +"";
}
/**
* @param context
* @return Android ID , 通常被认为不可信,因为它有时为null,无需任何许可
*/
privateStringgetAndroidID()
{
Stringm_szAndroidID ="";
if (Secure.getString(context.getContentResolver(),Secure.ANDROID_ID) !=null &&
!Secure.getString(context.getContentResolver(),Secure.ANDROID_ID).equals(null))
{
m_szAndroidID =Secure.getString(context.getContentResolver(),Secure.ANDROID_ID);
}
returnm_szAndroidID +"";
}
/**
* @param context
* @return WLAN MAC Address string, 是另一个唯一ID
* 加入android.permission.ACCESS_WIFI_STATE 权限,否则这个地址会为null //
* <uses-permission
* android:name="android.permission.ACCESS_WIFI_STATE" />
*/
privateStringgetSzWLANMAC()
{
WifiManagerwm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
Stringm_szWLANMAC ="";
if (wm.getConnectionInfo().getMacAddress() !=null && !wm.getConnectionInfo().getMacAddress().equals(null))
{
m_szWLANMAC =wm.getConnectionInfo().getMacAddress();
}
returnm_szWLANMAC;
}
/**
* @return BT MAC Address string, 只在有蓝牙的设备上运行 // <uses-permission
* android:name="android.permission.BLUETOOTH" />
*/
privateStringgetSzBTMAC()
{
BluetoothAdapterm_BluetoothAdapter =null;// Local Bluetooth adapter
m_BluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
Stringm_szBTMAC ="";
if (m_BluetoothAdapter.getAddress() !=null && !m_BluetoothAdapter.getAddress().equals(null))
{
m_szBTMAC =m_BluetoothAdapter.getAddress();
}
returnm_szBTMAC;
}
}
/////////通过上面设置的4个标准值,就可以比较准确的唯一标示一台Android设备了。
/*-----------下面调用------------*/
UpLoadDeviceToken upLoadDeviceToken = new UpLoadDeviceToken(this);
String deviceToken = upLoadDeviceToken.getDeviceToken();