安卓 获取设备id

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 08:33   80   0

获取设备id: DeviceInfo.getDeviceID(this);


import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;


public class DeviceInfo
{
 private static String sDeviceID;
 
 // <!-- 读取手机IMEI的权限,须在获得此权限后再初始化sdk,如果缺少此权限,会以AndroidID作为设备唯一标识符 -->
 // <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 
 /** 获取设备Id */
 public static String getDeviceID(Context context)
 {
  if ((sDeviceID != null) && (!"unknown".equals(sDeviceID))) return sDeviceID;
  
  try
  {
   TelephonyManager tm = (TelephonyManager) context.getSystemService("phone");
   sDeviceID = tm.getDeviceId();
   
   if ((sDeviceID == null) || (sDeviceID.length() == 0))
   {
    sDeviceID = getAndroidId(context);
   }
   
   return sDeviceID;
  }
  catch (Exception e)
  {}
  
  sDeviceID = getAndroidId(context);
  
  return sDeviceID;
 }
 
 /** 获取安卓Id */
 public static String getAndroidId(Context context)
 {
  String androidid = "unknown";
  try
  {
   androidid = Settings.Secure.getString(context.getContentResolver(), "android_id");
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  if ((androidid == null) || (androidid.length() == 0)) { return "unknown"; }
  return androidid;
 }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP