Java tm.getDeviceId() 已弃用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46744104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:27:48  来源:igfitidea点击:

tm.getDeviceId() is deprecated?

javaandroidandroid-8.0-oreotelephonymanager

提问by ravi

I'm getting the IMEIand device Id's, so here I am getting a problem getDeviceId()is deprecated.

我正在获取IMEI和设备 ID,所以在这里我遇到了一个问题getDeviceId()已被弃用。

TelephonyManager tm = (TelephonyManager) 
                 getSystemService(this.TELEPHONY_SERVICE);

imei = tm.getDeviceId();
device = Settings.Secure.getString(this.getContentResolver(),
           Settings.Secure.ANDROID_ID);

回答by Nilesh Rathod

getDeviceId()

getDeviceId()

Returns the unique device ID of a subscription, for example, the IMEI for GSM and the MEID for CDMA phones. Return null if device ID is not available.

This method was deprecatedin API level 26.

Use (@link getImei}which returns IMEIfor GSM

or (@link getMeid}which returns MEIDfor CDMA.

返回订阅的唯一设备 ID,例如,GSM 的 IMEI 和 CDMA 手机的 MEID。如果设备 ID 不可用,则返回 null。

此方法在 API 级别 26 中已弃用

使用(@link getImei}该回报IMEIGSM

(@link getMeid}返回MEIDCDMA

for more information read TelephonyManager

有关更多信息,请阅读TelephonyManager

Try this to get IMEI

试试这个来获取 IMEI

 @RequiresApi(api = Build.VERSION_CODES.O)
 TelephonyManager tm = (TelephonyManager)
            getSystemService(this.TELEPHONY_SERVICE);
    String imei = tm.getImei();

OR

或者

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String imei = telephonyMgr.getImei();
} else {
            String imei = telephonyMgr.getDeviceId();
}

Try this to get MEID

试试这个得到 MEID

@RequiresApi(api = Build.VERSION_CODES.O)
TelephonyManager tm = (TelephonyManager)
            getSystemService(this.TELEPHONY_SERVICE);

    String meid=tm.getMeid();

OR

或者

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String meid=tm.getMeid();
} 

回答by Bingerz

This is my solution:

这是我的解决方案:

@SuppressWarnings("deprecation")
private String getIMEINumber() {
    String IMEINumber = "";
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
        TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            IMEINumber = telephonyMgr.getImei();
        } else {
            IMEINumber = telephonyMgr.getDeviceId();
        }
    }
    return IMEINumber;
}

回答by Pradeep Sheoran

copy and paste my this program and understood. we face issue here only in Permission (Run time and Check Permission Type );- Now i complete it and paste here a accurate program:

复制并粘贴我的这个程序并理解。我们只在权限(运行时和检查权限类型)中遇到问题;- 现在我完成它并在此处粘贴一个准确的程序:

import android.*;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.UUID;

public class Login extends AppCompatActivity {

    private Button loginBtn;
    private TextView textView;
    private String IMEINumber;




    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        textView = (TextView) findViewById(R.id.textView);
        final int reqcode = 1;


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            String[] per = {Manifest.permission.READ_PHONE_STATE};
            requestPermissions(per, reqcode);
            loginBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                    if (ActivityCompat.checkSelfPermission(Login.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            IMEINumber = tm.getImei();
                            textView.setText(IMEINumber);
                        }
                    } else {
                        IMEINumber = tm.getDeviceId();
                        textView.setText(IMEINumber);
                    }
                }
            });


        }
    }

}

回答by Hamed Jaliliani

Kotlin Code for getting DeviceId ( IMEI ) with handling with permission & compatibility check for all android versions :

用于获取 DeviceId (IMEI) 的 Kotlin 代码,并对所有 android 版本进行许可和兼容性检查:

 val  telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
        == PackageManager.PERMISSION_GRANTED) {
        // Permission is  granted
        val imei : String? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             telephonyManager.imei
         } else { // older OS  versions
             telephonyManager.getDeviceId()
         }

        imei?.let {
            Log.i("Log", "DeviceId=$imei" )
        }

    } else {  // Permission is not granted

    }

Also add this permission to AndroidManifest.xml :

还要将此权限添加到 AndroidManifest.xml :

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- IMEI-->

回答by karim

Edit: Since Android 10, you cannot request IMEI and MEID unless you have READ_PRIVILEGED_PHONE_STATE permission

编辑:从 Android 10 开始,除非您拥有 READ_PRIVILEGED_PHONE_STATE 权限,否则您无法请求 IMEI 和 MEID

Should not it be something like this?

不应该是这样的吗?

            if (Build.VERSION.SDK_INT >= 26) {
                if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
                    deviceId = telMgr.getMeid();
                } else if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
                    deviceId = telMgr.getImei();
                } else {
                    deviceId = ""; // default!!!
                }
            } else {
                deviceId = telMgr.getDeviceId();
            }