Android中IMEI怎么替換為Android_id

這篇文章主要介紹了Android中IMEI怎么替換為Android_id的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Android中IMEI怎么替換為Android_id文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

創(chuàng)新互聯(lián)公司基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供綿陽(yáng)服務(wù)器托管 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。

前置工作:

項(xiàng)目配置升到對(duì)應(yīng)的29版本

compileSdkVersion: 29,
buildToolsVersion: ‘29.0.0',
minSdkVersion : 19,
targetSdkVersion : 29,
javaVersion : JavaVersion.VERSION_1_8

老版本獲取IMEI的方法:

public static String getIMEI(Context context) {
    String deviceId = null;
    try {
      TelephonyManager tm = (TelephonyManager) context
          .getSystemService(Context.TELEPHONY_SERVICE);
      deviceId = tm.getDeviceId();
      if (deviceId == null || "".equals(deviceId)) {
        return getLocalMacAddress(context);
      }
    } catch (Exception e) {
      e.printStackTrace();
      if (deviceId == null || "".equals(deviceId)) {
        return getLocalMacAddress(context);//獲取Mac地址,在Android 9 P版本中,地址會(huì)隨機(jī)變化,不可用作唯一標(biāo)識(shí),可去掉。
      }
    }

    return deviceId;
  }

Android Q獲取IMEI方法

public static String getIMEI(Context context) {
    String deviceId = null;
    try {
      TelephonyManager tm = (TelephonyManager) context
          .getSystemService(Context.TELEPHONY_SERVICE);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        deviceId = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
      } else {
        // request old storage permission
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
          // TODO: Consider calling
          //  ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //  public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                     int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          return null;
        }
        deviceId = tm.getDeviceId();
      }
      if (deviceId == null || "".equals(deviceId)) {
        return getLocalMacAddress(context);
      }
    } catch (Exception e) {
      e.printStackTrace();
      if (deviceId == null || "".equals(deviceId)) {
        return getLocalMacAddress(context);
      }
    }

    return deviceId;
  }

谷歌官方有聲明:手機(jī)恢復(fù)出廠設(shè)置,Android ID會(huì)重置。

如果用戶拒絕權(quán)限,也還是會(huì)獲取不到設(shè)備標(biāo)識(shí)。

關(guān)于“Android中IMEI怎么替換為Android_id”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Android中IMEI怎么替換為Android_id”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前標(biāo)題:Android中IMEI怎么替換為Android_id
鏈接分享:http://www.muchs.cn/article10/iidhdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、域名注冊(cè)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站維護(hù)定制網(wǎng)站、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)